home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / generic / tkMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-31  |  84.4 KB  |  2,782 lines

  1. /* 
  2.  * tkMenu.c --
  3.  *
  4.  *    This module implements menus for the Tk toolkit.  The menus
  5.  *    support normal button entries, plus check buttons, radio
  6.  *    buttons, iconic forms of all of the above, and separator
  7.  *    entries.
  8.  *
  9.  * Copyright (c) 1990-1994 The Regents of the University of California.
  10.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  11.  *
  12.  * See the file "license.terms" for information on usage and redistribution
  13.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14.  *
  15.  * SCCS: @(#) tkMenu.c 1.102 96/03/26 16:07:08
  16.  */
  17.  
  18. #include "tkPort.h"
  19. #include "default.h"
  20. #include "tkInt.h"
  21.  
  22. /*
  23.  * One of the following data structures is kept for each entry of each
  24.  * menu managed by this file:
  25.  */
  26.  
  27. typedef struct MenuEntry {
  28.     int type;            /* Type of menu entry;  see below for
  29.                  * valid types. */
  30.     struct Menu *menuPtr;    /* Menu with which this entry is associated. */
  31.     char *label;        /* Main text label displayed in entry (NULL
  32.                  * if no label).  Malloc'ed. */
  33.     int labelLength;        /* Number of non-NULL characters in label. */
  34.     int underline;        /* Index of character to underline. */
  35.     Pixmap bitmap;        /* Bitmap to display in menu entry, or None.
  36.                  * If not None then label is ignored. */
  37.     char *imageString;        /* Name of image to display (malloc'ed), or
  38.                  * NULL.  If non-NULL, bitmap, text, and
  39.                  * textVarName are ignored. */
  40.     Tk_Image image;        /* Image to display in menu entry, or NULL if
  41.                  * none. */
  42.     char *selectImageString;    /* Name of image to display when selected
  43.                  * (malloc'ed), or NULL. */
  44.     Tk_Image selectImage;    /* Image to display in entry when selected,
  45.                  * or NULL if none.  Ignored if image is
  46.                  * NULL. */
  47.     char *accel;        /* Accelerator string displayed at right
  48.                  * of menu entry.  NULL means no such
  49.                  * accelerator.  Malloc'ed. */
  50.     int accelLength;        /* Number of non-NULL characters in
  51.                  * accelerator. */
  52.  
  53.     /*
  54.      * Information related to displaying entry:
  55.      */
  56.  
  57.     Tk_Uid state;        /* State of button for display purposes:
  58.                  * normal, active, or disabled. */
  59.     int height;            /* Number of pixels occupied by entry in
  60.                  * vertical dimension, including raised
  61.                  * border drawn around entry when active. */
  62.     int y;            /* Y-coordinate of topmost pixel in entry. */
  63.     int indicatorOn;        /* True means draw indicator, false means
  64.                  * don't draw it. */
  65.     int indicatorDiameter;    /* Size of indicator display, in pixels. */
  66.     Tk_3DBorder border;        /* Structure used to draw background for
  67.                  * entry.  NULL means use overall border
  68.                  * for menu. */
  69.     XColor *fg;            /* Foreground color to use for entry.  NULL
  70.                  * means use foreground color from menu. */
  71.     Tk_3DBorder activeBorder;    /* Used to draw background and border when
  72.                  * element is active.  NULL means use
  73.                  * activeBorder from menu. */
  74.     XColor *activeFg;        /* Foreground color to use when entry is
  75.                  * active.  NULL means use active foreground
  76.                  * from menu. */
  77.     XFontStruct *fontPtr;    /* Text font for menu entries.  NULL means
  78.                  * use overall font for menu. */
  79.     GC textGC;            /* GC for drawing text in entry.  NULL means
  80.                  * use overall textGC for menu. */
  81.     GC activeGC;        /* GC for drawing text in entry when active.
  82.                  * NULL means use overall activeGC for
  83.                  * menu. */
  84.     GC disabledGC;        /* Used to produce disabled effect for entry.
  85.                  * NULL means use overall disabledGC from
  86.                  * menu structure.  See comments for
  87.                  * disabledFg in menu structure for more
  88.                  * information. */
  89.     XColor *indicatorFg;    /* Color for indicators in radio and check
  90.                  * button entries.  NULL means use indicatorFg
  91.                  * GC from menu. */
  92.     GC indicatorGC;        /* For drawing indicators.  None means use
  93.                  * GC from menu. */
  94.  
  95.     /*
  96.      * Information used to implement this entry's action:
  97.      */
  98.  
  99.     char *command;        /* Command to invoke when entry is invoked.
  100.                  * Malloc'ed. */
  101.     char *name;            /* Name of variable (for check buttons and
  102.                  * radio buttons) or menu (for cascade
  103.                  * entries).  Malloc'ed.*/
  104.     char *onValue;        /* Value to store in variable when selected
  105.                  * (only for radio and check buttons).
  106.                  * Malloc'ed. */
  107.     char *offValue;        /* Value to store in variable when not
  108.                  * selected (only for check buttons).
  109.                  * Malloc'ed. */
  110.  
  111.     /*
  112.      * Miscellaneous information:
  113.      */
  114.  
  115.     int flags;            /* Various flags.  See below for definitions. */
  116. } MenuEntry;
  117.  
  118. /*
  119.  * Flag values defined for menu entries:
  120.  *
  121.  * ENTRY_SELECTED:        Non-zero means this is a radio or check
  122.  *                button and that it should be drawn in
  123.  *                the "selected" state.
  124.  * ENTRY_NEEDS_REDISPLAY:    Non-zero means the entry should be redisplayed.
  125.  */
  126.  
  127. #define ENTRY_SELECTED        1
  128. #define ENTRY_NEEDS_REDISPLAY    4
  129.  
  130. /*
  131.  * Types defined for MenuEntries:
  132.  */
  133.  
  134. #define COMMAND_ENTRY        0
  135. #define SEPARATOR_ENTRY        1
  136. #define CHECK_BUTTON_ENTRY    2
  137. #define RADIO_BUTTON_ENTRY    3
  138. #define CASCADE_ENTRY        4
  139. #define TEAROFF_ENTRY        5
  140.  
  141. /*
  142.  * Mask bits for above types:
  143.  */
  144.  
  145. #define COMMAND_MASK        TK_CONFIG_USER_BIT
  146. #define SEPARATOR_MASK        (TK_CONFIG_USER_BIT << 1)
  147. #define CHECK_BUTTON_MASK    (TK_CONFIG_USER_BIT << 2)
  148. #define RADIO_BUTTON_MASK    (TK_CONFIG_USER_BIT << 3)
  149. #define CASCADE_MASK        (TK_CONFIG_USER_BIT << 4)
  150. #define TEAROFF_MASK        (TK_CONFIG_USER_BIT << 5)
  151. #define ALL_MASK        (COMMAND_MASK | SEPARATOR_MASK \
  152.     | CHECK_BUTTON_MASK | RADIO_BUTTON_MASK | CASCADE_MASK | TEAROFF_MASK)
  153.  
  154. /*
  155.  * Configuration specs for individual menu entries:
  156.  */
  157.  
  158. static Tk_ConfigSpec entryConfigSpecs[] = {
  159.     {TK_CONFIG_BORDER, "-activebackground", (char *) NULL, (char *) NULL,
  160.     DEF_MENU_ENTRY_ACTIVE_BG, Tk_Offset(MenuEntry, activeBorder),
  161.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  162.     |TK_CONFIG_NULL_OK},
  163.     {TK_CONFIG_COLOR, "-activeforeground", (char *) NULL, (char *) NULL,
  164.     DEF_MENU_ENTRY_ACTIVE_FG, Tk_Offset(MenuEntry, activeFg),
  165.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  166.     |TK_CONFIG_NULL_OK},
  167.     {TK_CONFIG_STRING, "-accelerator", (char *) NULL, (char *) NULL,
  168.     DEF_MENU_ENTRY_ACCELERATOR, Tk_Offset(MenuEntry, accel),
  169.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  170.     |TK_CONFIG_NULL_OK},
  171.     {TK_CONFIG_BORDER, "-background", (char *) NULL, (char *) NULL,
  172.     DEF_MENU_ENTRY_BG, Tk_Offset(MenuEntry, border),
  173.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  174.     |SEPARATOR_MASK|TEAROFF_MASK|TK_CONFIG_NULL_OK},
  175.     {TK_CONFIG_BITMAP, "-bitmap", (char *) NULL, (char *) NULL,
  176.     DEF_MENU_ENTRY_BITMAP, Tk_Offset(MenuEntry, bitmap),
  177.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  178.     |TK_CONFIG_NULL_OK},
  179. #ifdef STk_CODE
  180.     {TK_CONFIG_CLOSURE, "-command", (char *) NULL, (char *) NULL,
  181. #else
  182.     {TK_CONFIG_STRING, "-command", (char *) NULL, (char *) NULL,
  183. #endif
  184.     DEF_MENU_ENTRY_COMMAND, Tk_Offset(MenuEntry, command),
  185.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  186.     |TK_CONFIG_NULL_OK},
  187.     {TK_CONFIG_FONT, "-font", (char *) NULL, (char *) NULL,
  188.     DEF_MENU_ENTRY_FONT, Tk_Offset(MenuEntry, fontPtr),
  189.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  190.     |TK_CONFIG_NULL_OK},
  191.     {TK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
  192.     DEF_MENU_ENTRY_FG, Tk_Offset(MenuEntry, fg),
  193.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  194.     |TK_CONFIG_NULL_OK},
  195.     {TK_CONFIG_STRING, "-image", (char *) NULL, (char *) NULL,
  196.     DEF_MENU_ENTRY_IMAGE, Tk_Offset(MenuEntry, imageString),
  197.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  198.     |TK_CONFIG_NULL_OK},
  199.     {TK_CONFIG_BOOLEAN, "-indicatoron", (char *) NULL, (char *) NULL,
  200.     DEF_MENU_ENTRY_INDICATOR, Tk_Offset(MenuEntry, indicatorOn),
  201.     CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|TK_CONFIG_DONT_SET_DEFAULT},
  202.     {TK_CONFIG_STRING, "-label", (char *) NULL, (char *) NULL,
  203.     DEF_MENU_ENTRY_LABEL, Tk_Offset(MenuEntry, label),
  204.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK},
  205. #ifdef STk_CODE
  206.     {TK_CONFIG_MENU, "-menu", (char *) NULL, (char *) NULL,
  207. #else
  208.     {TK_CONFIG_STRING, "-menu", (char *) NULL, (char *) NULL,
  209. #endif
  210.     DEF_MENU_ENTRY_MENU, Tk_Offset(MenuEntry, name),
  211.     CASCADE_MASK|TK_CONFIG_NULL_OK},
  212.     {TK_CONFIG_STRING, "-offvalue", (char *) NULL, (char *) NULL,
  213.     DEF_MENU_ENTRY_OFF_VALUE, Tk_Offset(MenuEntry, offValue),
  214.     CHECK_BUTTON_MASK},
  215.     {TK_CONFIG_STRING, "-onvalue", (char *) NULL, (char *) NULL,
  216.     DEF_MENU_ENTRY_ON_VALUE, Tk_Offset(MenuEntry, onValue),
  217.     CHECK_BUTTON_MASK},
  218.     {TK_CONFIG_COLOR, "-selectcolor", (char *) NULL, (char *) NULL,
  219.     DEF_MENU_ENTRY_SELECT, Tk_Offset(MenuEntry, indicatorFg),
  220.     CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|TK_CONFIG_NULL_OK},
  221.     {TK_CONFIG_STRING, "-selectimage", (char *) NULL, (char *) NULL,
  222.     DEF_MENU_ENTRY_SELECT_IMAGE, Tk_Offset(MenuEntry, selectImageString),
  223.     CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|TK_CONFIG_NULL_OK},
  224.     {TK_CONFIG_UID, "-state", (char *) NULL, (char *) NULL,
  225.     DEF_MENU_ENTRY_STATE, Tk_Offset(MenuEntry, state),
  226.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  227.     |TEAROFF_MASK|TK_CONFIG_DONT_SET_DEFAULT},
  228.     {TK_CONFIG_STRING, "-value", (char *) NULL, (char *) NULL,
  229.     DEF_MENU_ENTRY_VALUE, Tk_Offset(MenuEntry, onValue),
  230.     RADIO_BUTTON_MASK|TK_CONFIG_NULL_OK},
  231.     {TK_CONFIG_STRING, "-variable", (char *) NULL, (char *) NULL,
  232.     DEF_MENU_ENTRY_CHECK_VARIABLE, Tk_Offset(MenuEntry, name),
  233.     CHECK_BUTTON_MASK|TK_CONFIG_NULL_OK},
  234.     {TK_CONFIG_STRING, "-variable", (char *) NULL, (char *) NULL,
  235.     DEF_MENU_ENTRY_RADIO_VARIABLE, Tk_Offset(MenuEntry, name),
  236.     RADIO_BUTTON_MASK},
  237.     {TK_CONFIG_INT, "-underline", (char *) NULL, (char *) NULL,
  238.     DEF_MENU_ENTRY_UNDERLINE, Tk_Offset(MenuEntry, underline),
  239.     COMMAND_MASK|CHECK_BUTTON_MASK|RADIO_BUTTON_MASK|CASCADE_MASK
  240.     |TK_CONFIG_DONT_SET_DEFAULT},
  241.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  242.     (char *) NULL, 0, 0}
  243. };
  244.  
  245. /*
  246.  * A data structure of the following type is kept for each
  247.  * menu managed by this file:
  248.  */
  249.  
  250. typedef struct Menu {
  251.     Tk_Window tkwin;        /* Window that embodies the pane.  NULL
  252.                  * means that the window has been destroyed
  253.                  * but the data structures haven't yet been
  254.                  * cleaned up.*/
  255.     Display *display;        /* Display containing widget.  Needed, among
  256.                  * other things, so that resources can be
  257.                  * freed up even after tkwin has gone away. */
  258.     Tcl_Interp *interp;        /* Interpreter associated with menu. */
  259.     Tcl_Command widgetCmd;    /* Token for menu's widget command. */
  260.     MenuEntry **entries;    /* Array of pointers to all the entries
  261.                  * in the menu.  NULL means no entries. */
  262.     int numEntries;        /* Number of elements in entries. */
  263.     int active;            /* Index of active entry.  -1 means
  264.                  * nothing active. */
  265.  
  266.     /*
  267.      * Information used when displaying widget:
  268.      */
  269.  
  270.     Tk_3DBorder border;        /* Structure used to draw 3-D
  271.                  * border and background for menu. */
  272.     int borderWidth;        /* Width of border around whole menu. */
  273.     Tk_3DBorder activeBorder;    /* Used to draw background and border for
  274.                  * active element (if any). */
  275.     int activeBorderWidth;    /* Width of border around active element. */
  276.     int relief;            /* 3-d effect: TK_RELIEF_RAISED, etc. */
  277.     XFontStruct *fontPtr;    /* Text font for menu entries. */
  278.     XColor *fg;            /* Foreground color for entries. */
  279.     GC textGC;            /* GC for drawing text and other features
  280.                  * of menu entries. */
  281.     XColor *disabledFg;        /* Foreground color when disabled.  NULL
  282.                  * means use normalFg with a 50% stipple
  283.                  * instead. */
  284.     Pixmap gray;        /* Bitmap for drawing disabled entries in
  285.                  * a stippled fashion.  None means not
  286.                  * allocated yet. */
  287.     GC disabledGC;        /* Used to produce disabled effect.  If
  288.                  * disabledFg isn't NULL, this GC is used to
  289.                  * draw text and icons for disabled entries.
  290.                  * Otherwise text and icons are drawn with
  291.                  * normalGC and this GC is used to stipple
  292.                  * background across them. */
  293.     XColor *activeFg;        /* Foreground color for active entry. */
  294.     GC activeGC;        /* GC for drawing active entry. */
  295.     XColor *indicatorFg;    /* Color for indicators in radio and check
  296.                  * button entries. */
  297.     GC indicatorGC;        /* For drawing indicators. */
  298.     int indicatorSpace;        /* Number of pixels to allow for displaying
  299.                  * indicators in menu entries (includes extra
  300.                  * space around indicator). */
  301.     int labelWidth;        /* Number of pixels to allow for displaying
  302.                  * labels in menu entries. */
  303.  
  304.     /*
  305.      * Miscellaneous information:
  306.      */
  307.  
  308.     int tearOff;        /* 1 means this is a tear-off menu, so the
  309.                  * first entry always shows a dashed stripe
  310.                  * for tearing off. */
  311.     char *tearOffCommand;    /* If non-NULL, points to a command to
  312.                  * run whenever the menu is torn-off. */
  313.     int transient;        /* 1 means menu is only posted briefly as
  314.                  * a popup or pulldown or cascade.  0 means
  315.                  * menu is always visible, e.g. as a torn-off
  316.                  * menu.  Determines whether save_under and
  317.                  * override_redirect should be set. */
  318.     Tk_Cursor cursor;        /* Current cursor for window, or None. */
  319.     char *takeFocus;        /* Value of -takefocus option;  not used in
  320.                  * the C code, but used by keyboard traversal
  321.                  * scripts.  Malloc'ed, but may be NULL. */
  322.     char *postCommand;        /* Command to execute just before posting
  323.                  * this menu, or NULL.  Malloc-ed. */
  324.     MenuEntry *postedCascade;    /* Points to menu entry for cascaded
  325.                  * submenu that is currently posted, or
  326.                  * NULL if no submenu posted. */
  327.     int flags;            /* Various flags;  see below for
  328.                  * definitions. */
  329. } Menu;
  330.  
  331. /*
  332.  * Flag bits for menus:
  333.  *
  334.  * REDRAW_PENDING:        Non-zero means a DoWhenIdle handler
  335.  *                has already been queued to redraw
  336.  *                this window.
  337.  * RESIZE_PENDING:        Non-zero means a call to ComputeMenuGeometry
  338.  *                has already been scheduled.
  339.  */
  340.  
  341. #define REDRAW_PENDING        1
  342. #define RESIZE_PENDING        2
  343.  
  344. /*
  345.  * Configuration specs valid for the menu as a whole:
  346.  */
  347.  
  348. static Tk_ConfigSpec configSpecs[] = {
  349.     {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
  350.     DEF_MENU_ACTIVE_BG_COLOR, Tk_Offset(Menu, activeBorder),
  351.     TK_CONFIG_COLOR_ONLY},
  352.     {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
  353.     DEF_MENU_ACTIVE_BG_MONO, Tk_Offset(Menu, activeBorder),
  354.     TK_CONFIG_MONO_ONLY},
  355.     {TK_CONFIG_PIXELS, "-activeborderwidth", "activeBorderWidth", "BorderWidth",
  356.     DEF_MENU_ACTIVE_BORDER_WIDTH, Tk_Offset(Menu, activeBorderWidth), 0},
  357.     {TK_CONFIG_COLOR, "-activeforeground", "activeForeground", "Background",
  358.     DEF_MENU_ACTIVE_FG_COLOR, Tk_Offset(Menu, activeFg),
  359.     TK_CONFIG_COLOR_ONLY},
  360.     {TK_CONFIG_COLOR, "-activeforeground", "activeForeground", "Background",
  361.     DEF_MENU_ACTIVE_FG_MONO, Tk_Offset(Menu, activeFg),
  362.     TK_CONFIG_MONO_ONLY},
  363.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  364.     DEF_MENU_BG_COLOR, Tk_Offset(Menu, border), TK_CONFIG_COLOR_ONLY},
  365.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  366.     DEF_MENU_BG_MONO, Tk_Offset(Menu, border), TK_CONFIG_MONO_ONLY},
  367.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  368.     (char *) NULL, 0, 0},
  369.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  370.     (char *) NULL, 0, 0},
  371.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  372.     DEF_MENU_BORDER_WIDTH, Tk_Offset(Menu, borderWidth), 0},
  373.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  374.     DEF_MENU_CURSOR, Tk_Offset(Menu, cursor), TK_CONFIG_NULL_OK},
  375.     {TK_CONFIG_COLOR, "-disabledforeground", "disabledForeground",
  376.     "DisabledForeground", DEF_MENU_DISABLED_FG_COLOR,
  377.     Tk_Offset(Menu, disabledFg), TK_CONFIG_COLOR_ONLY|TK_CONFIG_NULL_OK},
  378.     {TK_CONFIG_COLOR, "-disabledforeground", "disabledForeground",
  379.     "DisabledForeground", DEF_MENU_DISABLED_FG_MONO,
  380.     Tk_Offset(Menu, disabledFg), TK_CONFIG_MONO_ONLY|TK_CONFIG_NULL_OK},
  381.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  382.     (char *) NULL, 0, 0},
  383.     {TK_CONFIG_FONT, "-font", "font", "Font",
  384.     DEF_MENU_FONT, Tk_Offset(Menu, fontPtr), 0},
  385.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  386.     DEF_MENU_FG, Tk_Offset(Menu, fg), 0},
  387. #ifdef STk_CODE
  388.     {TK_CONFIG_CLOSURE, "-postcommand", "postCommand", "Command",
  389. #else
  390.     {TK_CONFIG_STRING, "-postcommand", "postCommand", "Command",
  391. #endif
  392.     DEF_MENU_POST_COMMAND, Tk_Offset(Menu, postCommand), TK_CONFIG_NULL_OK},
  393.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  394.     DEF_MENU_RELIEF, Tk_Offset(Menu, relief), 0},
  395.     {TK_CONFIG_COLOR, "-selectcolor", "selectColor", "Background",
  396.     DEF_MENU_SELECT_COLOR, Tk_Offset(Menu, indicatorFg),
  397.     TK_CONFIG_COLOR_ONLY},
  398.     {TK_CONFIG_COLOR, "-selectcolor", "selectColor", "Background",
  399.     DEF_MENU_SELECT_MONO, Tk_Offset(Menu, indicatorFg),
  400.     TK_CONFIG_MONO_ONLY},
  401. #ifdef STk_CODE
  402.     {TK_CONFIG_CLOSURE, "-takefocus", "takeFocus", "TakeFocus",
  403. #else
  404.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  405. #endif
  406.     DEF_MENU_TAKE_FOCUS, Tk_Offset(Menu, takeFocus), TK_CONFIG_NULL_OK},
  407.     {TK_CONFIG_BOOLEAN, "-tearoff", "tearOff", "TearOff",
  408.     DEF_MENU_TEAROFF, Tk_Offset(Menu, tearOff), 0},
  409. #ifdef STk_CODE
  410.     {TK_CONFIG_CLOSURE, "-tearoffcommand", "tearOffCommand", "TearOffCommand",
  411. #else
  412.     {TK_CONFIG_STRING, "-tearoffcommand", "tearOffCommand", "TearOffCommand",
  413. #endif
  414.     DEF_MENU_TEAROFF_CMD, Tk_Offset(Menu, tearOffCommand),
  415.     TK_CONFIG_NULL_OK},
  416.     {TK_CONFIG_BOOLEAN, "-transient", "transient", "Transient",
  417.     DEF_MENU_TRANSIENT, Tk_Offset(Menu, transient), 0},
  418.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  419.     (char *) NULL, 0, 0}
  420. };
  421.  
  422. /*
  423.  * Various geometry definitions:
  424.  */
  425.  
  426. #define CASCADE_ARROW_HEIGHT 10
  427. #define CASCADE_ARROW_WIDTH 8
  428. #define DECORATION_BORDER_WIDTH 2
  429. #define MARGIN_WIDTH 2
  430.  
  431. /*
  432.  * Forward declarations for procedures defined later in this file:
  433.  */
  434.  
  435. static int        ActivateMenuEntry _ANSI_ARGS_((Menu *menuPtr,
  436.                 int index));
  437. static void        ComputeMenuGeometry _ANSI_ARGS_((
  438.                 ClientData clientData));
  439. static int        ConfigureMenu _ANSI_ARGS_((Tcl_Interp *interp,
  440.                 Menu *menuPtr, int argc, char **argv,
  441.                 int flags));
  442. static int        ConfigureMenuEntry _ANSI_ARGS_((Tcl_Interp *interp,
  443.                 Menu *menuPtr, MenuEntry *mePtr, int index,
  444.                 int argc, char **argv, int flags));
  445. static void        DestroyMenu _ANSI_ARGS_((char *memPtr));
  446. static void        DestroyMenuEntry _ANSI_ARGS_((char *memPtr));
  447. static void        DisplayMenu _ANSI_ARGS_((ClientData clientData));
  448. static void        EventuallyRedrawMenu _ANSI_ARGS_((Menu *menuPtr,
  449.                 MenuEntry *mePtr));
  450. static int        GetMenuIndex _ANSI_ARGS_((Tcl_Interp *interp,
  451.                 Menu *menuPtr, char *string, int lastOK,
  452.                 int *indexPtr));
  453. static int        MenuAddOrInsert _ANSI_ARGS_((Tcl_Interp *interp,
  454.                 Menu *menuPtr, char *indexString, int argc,
  455.                 char **argv));
  456. static void        MenuCmdDeletedProc _ANSI_ARGS_((
  457.                 ClientData clientData));
  458. static void        MenuEventProc _ANSI_ARGS_((ClientData clientData,
  459.                 XEvent *eventPtr));
  460. static void        MenuImageProc _ANSI_ARGS_((ClientData clientData,
  461.                 int x, int y, int width, int height, int imgWidth,
  462.                 int imgHeight));
  463. static MenuEntry *    MenuNewEntry _ANSI_ARGS_((Menu *menuPtr, int index,
  464.                 int type));
  465. static void        MenuSelectImageProc _ANSI_ARGS_((ClientData clientData,
  466.                 int x, int y, int width, int height, int imgWidth,
  467.                 int imgHeight));
  468. static char *        MenuVarProc _ANSI_ARGS_((ClientData clientData,
  469.                 Tcl_Interp *interp, char *name1, char *name2,
  470.                 int flags));
  471. static int        MenuWidgetCmd _ANSI_ARGS_((ClientData clientData,
  472.                 Tcl_Interp *interp, int argc, char **argv));
  473. static int        PostSubmenu _ANSI_ARGS_((Tcl_Interp *interp,
  474.                 Menu *menuPtr, MenuEntry *mePtr));
  475.  
  476. /*
  477.  *--------------------------------------------------------------
  478.  *
  479.  * Tk_MenuCmd --
  480.  *
  481.  *    This procedure is invoked to process the "menu" Tcl
  482.  *    command.  See the user documentation for details on
  483.  *    what it does.
  484.  *
  485.  * Results:
  486.  *    A standard Tcl result.
  487.  *
  488.  * Side effects:
  489.  *    See the user documentation.
  490.  *
  491.  *--------------------------------------------------------------
  492.  */
  493.  
  494. int
  495. Tk_MenuCmd(clientData, interp, argc, argv)
  496.     ClientData clientData;    /* Main window associated with
  497.                  * interpreter. */
  498.     Tcl_Interp *interp;        /* Current interpreter. */
  499.     int argc;            /* Number of arguments. */
  500.     char **argv;        /* Argument strings. */
  501. {
  502.     Tk_Window tkwin = (Tk_Window) clientData;
  503.     Tk_Window new;
  504.     register Menu *menuPtr;
  505.  
  506.     if (argc < 2) {
  507.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  508.         argv[0], " pathName ?options?\"", (char *) NULL);
  509.     return TCL_ERROR;
  510.     }
  511.  
  512.     /*
  513.      * Create the new window.  Set override-redirect so the window
  514.      * manager won't add a border or argue about placement, and set
  515.      * save-under so that the window can pop up and down without a
  516.      * lot of re-drawing.
  517.      */
  518.  
  519.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], "");
  520.     if (new == NULL) {
  521.     return TCL_ERROR;
  522.     }
  523.  
  524.     /*
  525.      * Initialize the data structure for the menu.
  526.      */
  527.  
  528.     menuPtr = (Menu *) ckalloc(sizeof(Menu));
  529.     menuPtr->tkwin = new;
  530.     menuPtr->display = Tk_Display(new);
  531.     menuPtr->interp = interp;
  532.     menuPtr->widgetCmd = Tcl_CreateCommand(interp,
  533.         Tk_PathName(menuPtr->tkwin), MenuWidgetCmd,
  534.         (ClientData) menuPtr, MenuCmdDeletedProc);
  535.     menuPtr->entries = NULL;
  536.     menuPtr->numEntries = 0;
  537.     menuPtr->active = -1;
  538.     menuPtr->border = NULL;
  539.     menuPtr->borderWidth = 0;
  540.     menuPtr->relief = TK_RELIEF_FLAT;
  541.     menuPtr->activeBorder = NULL;
  542.     menuPtr->activeBorderWidth = 0;
  543.     menuPtr->fontPtr = NULL;
  544.     menuPtr->fg = NULL;
  545.     menuPtr->textGC = None;
  546.     menuPtr->disabledFg = NULL;
  547.     menuPtr->gray = None;
  548.     menuPtr->disabledGC = None;
  549.     menuPtr->activeFg = NULL;
  550.     menuPtr->activeGC = None;
  551.     menuPtr->indicatorFg = NULL;
  552.     menuPtr->indicatorGC = None;
  553.     menuPtr->indicatorSpace = 0;
  554.     menuPtr->labelWidth = 0;
  555.     menuPtr->tearOff = 1;
  556.     menuPtr->tearOffCommand = NULL;
  557.     menuPtr->cursor = None;
  558.     menuPtr->takeFocus = NULL;
  559.     menuPtr->postCommand = NULL;
  560.     menuPtr->postedCascade = NULL;
  561.     menuPtr->flags = 0;
  562.  
  563.     Tk_SetClass(new, "Menu");
  564.     Tk_CreateEventHandler(menuPtr->tkwin, ExposureMask|StructureNotifyMask,
  565.         MenuEventProc, (ClientData) menuPtr);
  566.     if (ConfigureMenu(interp, menuPtr, argc-2, argv+2, 0) != TCL_OK) {
  567.     goto error;
  568.     }
  569.  
  570. #ifdef STk_CODE
  571.     STk_sharp_dot_result(interp, Tk_PathName(menuPtr->tkwin));
  572. #else
  573.     interp->result = Tk_PathName(menuPtr->tkwin);
  574. #endif
  575.     return TCL_OK;
  576.  
  577.     error:
  578.     Tk_DestroyWindow(menuPtr->tkwin);
  579.     return TCL_ERROR;
  580. }
  581.  
  582. /*
  583.  *--------------------------------------------------------------
  584.  *
  585.  * MenuWidgetCmd --
  586.  *
  587.  *    This procedure is invoked to process the Tcl command
  588.  *    that corresponds to a widget managed by this module.
  589.  *    See the user documentation for details on what it does.
  590.  *
  591.  * Results:
  592.  *    A standard Tcl result.
  593.  *
  594.  * Side effects:
  595.  *    See the user documentation.
  596.  *
  597.  *--------------------------------------------------------------
  598.  */
  599.  
  600. static int
  601. MenuWidgetCmd(clientData, interp, argc, argv)
  602.     ClientData clientData;    /* Information about menu widget. */
  603.     Tcl_Interp *interp;        /* Current interpreter. */
  604.     int argc;            /* Number of arguments. */
  605.     char **argv;        /* Argument strings. */
  606. {
  607.     register Menu *menuPtr = (Menu *) clientData;
  608.     register MenuEntry *mePtr;
  609.     int result = TCL_OK;
  610.     size_t length;
  611.     int c;
  612.  
  613.     if (argc < 2) {
  614.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  615.         argv[0], " option ?arg arg ...?\"", (char *) NULL);
  616.     return TCL_ERROR;
  617.     }
  618.     Tcl_Preserve((ClientData) menuPtr);
  619.     c = argv[1][0];
  620.     length = strlen(argv[1]);
  621.     if ((c == 'a') && (strncmp(argv[1], "activate", length) == 0)
  622.         && (length >= 2)) {
  623.     int index;
  624.  
  625.     if (argc != 3) {
  626.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  627.             argv[0], " activate index\"", (char *) NULL);
  628.         goto error;
  629.     }
  630.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  631.         goto error;
  632.     }
  633.     if (menuPtr->active == index) {
  634.         goto done;
  635.     }
  636.     if (index >= 0) {
  637.         if ((menuPtr->entries[index]->type == SEPARATOR_ENTRY)
  638.             || (menuPtr->entries[index]->state == tkDisabledUid)) {
  639.         index = -1;
  640.         }
  641.     }
  642.     result = ActivateMenuEntry(menuPtr, index);
  643.     } else if ((c == 'a') && (strncmp(argv[1], "add", length) == 0)
  644.         && (length >= 2)) {
  645.     if (argc < 3) {
  646.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  647.             argv[0], " add type ?options?\"", (char *) NULL);
  648.         goto error;
  649.     }
  650.     if (MenuAddOrInsert(interp, menuPtr, (char *) NULL,
  651.         argc-2, argv+2) != TCL_OK) {
  652.         goto error;
  653.     }
  654.     } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  655.         && (length >= 2)) {
  656.     if (argc != 3) {
  657.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  658.             argv[0], " cget option\"",
  659.             (char *) NULL);
  660.         goto error;
  661.     }
  662.     result = Tk_ConfigureValue(interp, menuPtr->tkwin, configSpecs,
  663.         (char *) menuPtr, argv[2], 0);
  664.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  665.         && (length >= 2)) {
  666.     if (argc == 2) {
  667.         result = Tk_ConfigureInfo(interp, menuPtr->tkwin, configSpecs,
  668.             (char *) menuPtr, (char *) NULL, 0);
  669.     } else if (argc == 3) {
  670.         result = Tk_ConfigureInfo(interp, menuPtr->tkwin, configSpecs,
  671.             (char *) menuPtr, argv[2], 0);
  672.     } else {
  673.         result = ConfigureMenu(interp, menuPtr, argc-2, argv+2,
  674.             TK_CONFIG_ARGV_ONLY);
  675.     }
  676.     } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
  677.     int first, last, i, numDeleted;
  678.  
  679.     if ((argc != 3) && (argc != 4)) {
  680.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  681.             argv[0], " delete first ?last?\"", (char *) NULL);
  682.         goto error;
  683.     }
  684.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &first) != TCL_OK) {
  685.         goto error;
  686.     }
  687.     if (argc == 3) {
  688.         last = first;
  689.     } else {
  690.         if (GetMenuIndex(interp, menuPtr, argv[3], 0, &last) != TCL_OK) {
  691.             goto error;
  692.         }
  693.     }
  694.     if (menuPtr->tearOff && (first == 0)) {
  695.         /*
  696.          * Sorry, can't delete the tearoff entry;  must reconfigure
  697.          * the menu.
  698.          */
  699.         first = 1;
  700.     }
  701.     if ((first < 0) || (last < first)) {
  702.         goto done;
  703.     }
  704.     numDeleted = last + 1 - first;
  705.     for (i = first; i <= last; i++) {
  706.         Tcl_EventuallyFree((ClientData) menuPtr->entries[i],
  707.                   DestroyMenuEntry);
  708.     }
  709.     for (i = last+1; i < menuPtr->numEntries; i++) {
  710.         menuPtr->entries[i-numDeleted] = menuPtr->entries[i];
  711.     }
  712.     menuPtr->numEntries -= numDeleted;
  713.     if ((menuPtr->active >= first) && (menuPtr->active <= last)) {
  714.         menuPtr->active = -1;
  715.     } else if (menuPtr->active > last) {
  716.         menuPtr->active -= numDeleted;
  717.     }
  718.     if (!(menuPtr->flags & RESIZE_PENDING)) {
  719.         menuPtr->flags |= RESIZE_PENDING;
  720.         Tcl_DoWhenIdle(ComputeMenuGeometry, (ClientData) menuPtr);
  721.     }
  722.     } else if ((c == 'e') && (length >= 7)
  723.         && (strncmp(argv[1], "entrycget", length) == 0)) {
  724.     int index;
  725.  
  726.     if (argc != 4) {
  727.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  728.             argv[0], " entrycget index option\"",
  729.             (char *) NULL);
  730.         goto error;
  731.     }
  732.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  733.         goto error;
  734.     }
  735.     if (index < 0) {
  736.         goto done;
  737.     }
  738.     mePtr = menuPtr->entries[index];
  739.     Tcl_Preserve((ClientData) mePtr);
  740.     result = Tk_ConfigureValue(interp, menuPtr->tkwin, entryConfigSpecs,
  741.         (char *) mePtr, argv[3], COMMAND_MASK << mePtr->type);
  742.     Tcl_Release((ClientData) mePtr);
  743.     } else if ((c == 'e') && (length >= 7)
  744.         && (strncmp(argv[1], "entryconfigure", length) == 0)) {
  745.     int index;
  746.  
  747.     if (argc < 3) {
  748.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  749.             argv[0], " entryconfigure index ?option value ...?\"",
  750.             (char *) NULL);
  751.         goto error;
  752.     }
  753.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  754.         goto error;
  755.     }
  756.     if (index < 0) {
  757.         goto done;
  758.     }
  759.     mePtr = menuPtr->entries[index];
  760.     Tcl_Preserve((ClientData) mePtr);
  761.     if (argc == 3) {
  762.         result = Tk_ConfigureInfo(interp, menuPtr->tkwin, entryConfigSpecs,
  763.             (char *) mePtr, (char *) NULL,
  764.             COMMAND_MASK << mePtr->type);
  765.     } else if (argc == 4) {
  766.         result = Tk_ConfigureInfo(interp, menuPtr->tkwin, entryConfigSpecs,
  767.             (char *) mePtr, argv[3], COMMAND_MASK << mePtr->type);
  768.     } else {
  769.         result = ConfigureMenuEntry(interp, menuPtr, mePtr, index, argc-3,
  770.             argv+3, TK_CONFIG_ARGV_ONLY | COMMAND_MASK << mePtr->type);
  771.     }
  772.     Tcl_Release((ClientData) mePtr);
  773.     } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
  774.         && (length >= 3)) {
  775.     int index;
  776.  
  777.     if (argc != 3) {
  778.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  779.             argv[0], " index string\"", (char *) NULL);
  780.         goto error;
  781.     }
  782.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  783.         goto error;
  784.     }
  785.     if (index < 0) {
  786. #ifdef STk_CODE
  787.         interp->result = "\"none\"";
  788. #else
  789.         interp->result = "none";
  790. #endif
  791.     } else {
  792.         sprintf(interp->result, "%d", index);
  793.     }
  794.     } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
  795.         && (length >= 3)) {
  796.     if (argc < 4) {
  797.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  798.             argv[0], " insert index type ?options?\"", (char *) NULL);
  799.         goto error;
  800.     }
  801.     if (MenuAddOrInsert(interp, menuPtr, argv[2],
  802.         argc-3, argv+3) != TCL_OK) {
  803.         goto error;
  804.     }
  805.     } else if ((c == 'i') && (strncmp(argv[1], "invoke", length) == 0)
  806.         && (length >= 3)) {
  807.     int index;
  808.  
  809.     if (argc != 3) {
  810.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  811.             argv[0], " invoke index\"", (char *) NULL);
  812.         goto error;
  813.     }
  814.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  815.         goto error;
  816.     }
  817.     if (index < 0) {
  818.         goto done;
  819.     }
  820.     mePtr = menuPtr->entries[index];
  821.     if (mePtr->state == tkDisabledUid) {
  822.         goto done;
  823.     }
  824.     Tcl_Preserve((ClientData) mePtr);
  825.     if (mePtr->type == CHECK_BUTTON_ENTRY) {
  826.         if (mePtr->flags & ENTRY_SELECTED) {
  827.         if (Tcl_SetVar(interp, mePtr->name, mePtr->offValue,
  828.             TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG) == NULL) {
  829.             result = TCL_ERROR;
  830.         }
  831.         } else {
  832.         if (Tcl_SetVar(interp, mePtr->name, mePtr->onValue,
  833.             TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG) == NULL) {
  834.             result = TCL_ERROR;
  835.         }
  836.         }
  837.     } else if (mePtr->type == RADIO_BUTTON_ENTRY) {
  838.         if (Tcl_SetVar(interp, mePtr->name, mePtr->onValue,
  839.             TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG) == NULL) {
  840.         result = TCL_ERROR;
  841.         }
  842.     }
  843.     if ((result == TCL_OK) && (mePtr->command != NULL)) {
  844.         result = TkCopyAndGlobalEval(interp, mePtr->command);
  845.     }
  846.     if ((result == TCL_OK) && (mePtr->type == CASCADE_ENTRY)) {
  847.         result = PostSubmenu(menuPtr->interp, menuPtr, mePtr);
  848.     }
  849.     Tcl_Release((ClientData) mePtr);
  850.     } else if ((c == 'p') && (strncmp(argv[1], "post", length) == 0)
  851.         && (length == 4)) {
  852.     int x, y, tmp, vRootX, vRootY;
  853.     int vRootWidth, vRootHeight;
  854.  
  855.     if (argc != 4) {
  856.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  857.             argv[0], " post x y\"", (char *) NULL);
  858.         goto error;
  859.     }
  860.     if ((Tcl_GetInt(interp, argv[2], &x) != TCL_OK)
  861.         || (Tcl_GetInt(interp, argv[3], &y) != TCL_OK)) {
  862.         goto error;
  863.     }
  864.  
  865.     /*
  866.      * De-activate any active element.
  867.      */
  868.  
  869.     ActivateMenuEntry(menuPtr, -1);
  870.  
  871.     /*
  872.      * If there is a command for the menu, execute it.  This
  873.      * may change the size of the menu, so be sure to recompute
  874.      * the menu's geometry if needed.
  875.      */
  876.  
  877.     if (menuPtr->postCommand != NULL) {
  878.         result = TkCopyAndGlobalEval(menuPtr->interp,
  879.             menuPtr->postCommand);
  880.         if (result != TCL_OK) {
  881.         return result;
  882.         }
  883.         if (menuPtr->flags & RESIZE_PENDING) {
  884.         Tcl_CancelIdleCall(ComputeMenuGeometry, (ClientData) menuPtr);
  885.         ComputeMenuGeometry((ClientData) menuPtr);
  886.         }
  887.     }
  888.  
  889.     /*
  890.      * Adjust the position of the menu if necessary to keep it
  891.      * visible on the screen.  There are two special tricks to
  892.      * make this work right:
  893.      *
  894.      * 1. If a virtual root window manager is being used then
  895.      *    the coordinates are in the virtual root window of
  896.      *    menuPtr's parent;  since the menu uses override-redirect
  897.      *    mode it will be in the *real* root window for the screen,
  898.      *    so we have to map the coordinates from the virtual root
  899.      *    (if any) to the real root.  Can't get the virtual root
  900.      *    from the menu itself (it will never be seen by the wm)
  901.      *    so use its parent instead (it would be better to have an
  902.      *    an option that names a window to use for this...).
  903.      * 2. The menu may not have been mapped yet, so its current size
  904.      *    might be the default 1x1.  To compute how much space it
  905.      *    needs, use its requested size, not its actual size.
  906.      */
  907.  
  908.     Tk_GetVRootGeometry(Tk_Parent(menuPtr->tkwin), &vRootX, &vRootY,
  909.         &vRootWidth, &vRootHeight);
  910.     x += vRootX;
  911.     y += vRootY;
  912.     tmp = WidthOfScreen(Tk_Screen(menuPtr->tkwin))
  913.         - Tk_ReqWidth(menuPtr->tkwin);
  914.     if (x > tmp) {
  915.         x = tmp;
  916.     }
  917.     if (x < 0) {
  918.         x = 0;
  919.     }
  920.     tmp = HeightOfScreen(Tk_Screen(menuPtr->tkwin))
  921.         - Tk_ReqHeight(menuPtr->tkwin);
  922.     if (y > tmp) {
  923.         y = tmp;
  924.     }
  925.     if (y < 0) {
  926.         y = 0;
  927.     }
  928.     if ((x != Tk_X(menuPtr->tkwin)) || (y != Tk_Y(menuPtr->tkwin))) {
  929.         Tk_MoveToplevelWindow(menuPtr->tkwin, x, y);
  930.     }
  931.     if (!Tk_IsMapped(menuPtr->tkwin)) {
  932.         Tk_MapWindow(menuPtr->tkwin);
  933.     }
  934.     XRaiseWindow(menuPtr->display, Tk_WindowId(menuPtr->tkwin));
  935.     } else if ((c == 'p') && (strncmp(argv[1], "postcascade", length) == 0)
  936.         && (length > 4)) {
  937.     int index;
  938.     if (argc != 3) {
  939.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  940.             argv[0], " postcascade index\"", (char *) NULL);
  941.         goto error;
  942.     }
  943.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  944.         goto error;
  945.     }
  946.     if ((index < 0) || (menuPtr->entries[index]->type != CASCADE_ENTRY)) {
  947.         result = PostSubmenu(interp, menuPtr, (MenuEntry *) NULL);
  948.     } else {
  949.         result = PostSubmenu(interp, menuPtr, menuPtr->entries[index]);
  950.     }
  951.     } else if ((c == 't') && (strncmp(argv[1], "type", length) == 0)) {
  952.     int index;
  953.     if (argc != 3) {
  954.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  955.             argv[0], " type index\"", (char *) NULL);
  956.         goto error;
  957.     }
  958.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  959.         goto error;
  960.     }
  961.     if (index < 0) {
  962.         goto done;
  963.     }
  964.     mePtr = menuPtr->entries[index];
  965.     switch (mePtr->type) {
  966. #ifdef STk_CODE
  967.         case COMMAND_ENTRY:
  968.         interp->result = "\"command\"";
  969.         break;
  970.         case SEPARATOR_ENTRY:
  971.         interp->result = "\"separator\"";
  972.         break;
  973.         case CHECK_BUTTON_ENTRY:
  974.         interp->result = "\"checkbutton\"";
  975.         break;
  976.         case RADIO_BUTTON_ENTRY:
  977.         interp->result = "\"radiobutton\"";
  978.         break;
  979.         case CASCADE_ENTRY:
  980.         interp->result = "\"cascade\"";
  981.         break;
  982.         case TEAROFF_ENTRY:
  983.         interp->result = "\"tearoff\"";
  984.         break;
  985. #else
  986.         case COMMAND_ENTRY:
  987.         interp->result = "command";
  988.         break;
  989.         case SEPARATOR_ENTRY:
  990.         interp->result = "separator";
  991.         break;
  992.         case CHECK_BUTTON_ENTRY:
  993.         interp->result = "checkbutton";
  994.         break;
  995.         case RADIO_BUTTON_ENTRY:
  996.         interp->result = "radiobutton";
  997.         break;
  998.         case CASCADE_ENTRY:
  999.         interp->result = "cascade";
  1000.         break;
  1001.         case TEAROFF_ENTRY:
  1002.         interp->result = "tearoff";
  1003.         break;
  1004. #endif
  1005.     }
  1006.     } else if ((c == 'u') && (strncmp(argv[1], "unpost", length) == 0)) {
  1007.     if (argc != 2) {
  1008.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1009.             argv[0], " unpost\"", (char *) NULL);
  1010.         goto error;
  1011.     }
  1012.     Tk_UnmapWindow(menuPtr->tkwin);
  1013.     if (result == TCL_OK) {
  1014.         result = PostSubmenu(interp, menuPtr, (MenuEntry *) NULL);
  1015.     }
  1016.     } else if ((c == 'y') && (strncmp(argv[1], "yposition", length) == 0)) {
  1017.     int index;
  1018.  
  1019.     if (argc != 3) {
  1020.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1021.             argv[0], " yposition index\"", (char *) NULL);
  1022.         goto error;
  1023.     }
  1024.     if (GetMenuIndex(interp, menuPtr, argv[2], 0, &index) != TCL_OK) {
  1025.         goto error;
  1026.     }
  1027.     if (index < 0) {
  1028.         interp->result = "0";
  1029.     } else {
  1030.         sprintf(interp->result, "%d", menuPtr->entries[index]->y);
  1031.     }
  1032.     } else {
  1033.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  1034.         "\": must be activate, add, cget, configure, delete, ",
  1035.         "entrycget, entryconfigure, index, insert, invoke, ",
  1036.         "post, postcascade, type, unpost, or yposition",
  1037.         (char *) NULL);
  1038.     goto error;
  1039.     }
  1040.     done:
  1041.     Tcl_Release((ClientData) menuPtr);
  1042.     return result;
  1043.  
  1044.     error:
  1045.     Tcl_Release((ClientData) menuPtr);
  1046.     return TCL_ERROR;
  1047. }
  1048.  
  1049. /*
  1050.  *----------------------------------------------------------------------
  1051.  *
  1052.  * DestroyMenu --
  1053.  *
  1054.  *    This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
  1055.  *    to clean up the internal structure of a menu at a safe time
  1056.  *    (when no-one is using it anymore).
  1057.  *
  1058.  * Results:
  1059.  *    None.
  1060.  *
  1061.  * Side effects:
  1062.  *    Everything associated with the menu is freed up.
  1063.  *
  1064.  *----------------------------------------------------------------------
  1065.  */
  1066.  
  1067. static void
  1068. DestroyMenu(memPtr)
  1069.     char *memPtr;    /* Info about menu widget. */
  1070. {
  1071.     register Menu *menuPtr = (Menu *) memPtr;
  1072.     int i;
  1073.  
  1074.     /*
  1075.      * Free up all the stuff that requires special handling, then
  1076.      * let Tk_FreeOptions handle all the standard option-related
  1077.      * stuff.
  1078.      */
  1079.  
  1080.     for (i = 0; i < menuPtr->numEntries; i++) {
  1081.     DestroyMenuEntry((char *) menuPtr->entries[i]);
  1082.     }
  1083.     if (menuPtr->entries != NULL) {
  1084.     ckfree((char *) menuPtr->entries);
  1085.     }
  1086.     if (menuPtr->textGC != None) {
  1087.     Tk_FreeGC(menuPtr->display, menuPtr->textGC);
  1088.     }
  1089.     if (menuPtr->gray != None) {
  1090.     Tk_FreeBitmap(menuPtr->display, menuPtr->gray);
  1091.     }
  1092.     if (menuPtr->disabledGC != None) {
  1093.     Tk_FreeGC(menuPtr->display, menuPtr->disabledGC);
  1094.     }
  1095.     if (menuPtr->activeGC != None) {
  1096.     Tk_FreeGC(menuPtr->display, menuPtr->activeGC);
  1097.     }
  1098.     if (menuPtr->indicatorGC != None) {
  1099.     Tk_FreeGC(menuPtr->display, menuPtr->indicatorGC);
  1100.     }
  1101.     Tk_FreeOptions(configSpecs, (char *) menuPtr, menuPtr->display, 0);
  1102.     ckfree((char *) menuPtr);
  1103. }
  1104.  
  1105. /*
  1106.  *----------------------------------------------------------------------
  1107.  *
  1108.  * DestroyMenuEntry --
  1109.  *
  1110.  *    This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
  1111.  *    to clean up the internal structure of a menu entry at a safe time
  1112.  *    (when no-one is using it anymore).
  1113.  *
  1114.  * Results:
  1115.  *    None.
  1116.  *
  1117.  * Side effects:
  1118.  *    Everything associated with the menu entry is freed up.
  1119.  *
  1120.  *----------------------------------------------------------------------
  1121.  */
  1122.  
  1123. static void
  1124. DestroyMenuEntry(memPtr)
  1125.     char *memPtr;        /* Pointer to entry to be freed. */
  1126. {
  1127.     register MenuEntry *mePtr = (MenuEntry *) memPtr;
  1128.     Menu *menuPtr = mePtr->menuPtr;
  1129.  
  1130.     /*
  1131.      * Free up all the stuff that requires special handling, then
  1132.      * let Tk_FreeOptions handle all the standard option-related
  1133.      * stuff.
  1134.      */
  1135.  
  1136.     if (menuPtr->postedCascade == mePtr) {
  1137.     /*
  1138.      * Ignore errors while unposting the menu, since it's possible
  1139.      * that the menu has already been deleted and the unpost will
  1140.      * generate an error.
  1141.      */
  1142.  
  1143.     PostSubmenu(menuPtr->interp, menuPtr, (MenuEntry *) NULL);
  1144.     }
  1145.     if (mePtr->image != NULL) {
  1146.     Tk_FreeImage(mePtr->image);
  1147.     }
  1148.     if (mePtr->textGC != None) {
  1149.     Tk_FreeGC(menuPtr->display, mePtr->textGC);
  1150.     }
  1151.     if (mePtr->activeGC != None) {
  1152.     Tk_FreeGC(menuPtr->display, mePtr->activeGC);
  1153.     }
  1154.     if (mePtr->disabledGC != None) {
  1155.     Tk_FreeGC(menuPtr->display, mePtr->disabledGC);
  1156.     }
  1157.     if (mePtr->indicatorGC != None) {
  1158.     Tk_FreeGC(menuPtr->display, mePtr->indicatorGC);
  1159.     }
  1160.     if (mePtr->name != NULL) {
  1161.     Tcl_UntraceVar(menuPtr->interp, mePtr->name,
  1162.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  1163.         MenuVarProc, (ClientData) mePtr);
  1164.     }
  1165.     Tk_FreeOptions(entryConfigSpecs, (char *) mePtr, menuPtr->display, 
  1166.         (COMMAND_MASK << mePtr->type));
  1167.     ckfree((char *) mePtr);
  1168. }
  1169.  
  1170. /*
  1171.  *----------------------------------------------------------------------
  1172.  *
  1173.  * ConfigureMenu --
  1174.  *
  1175.  *    This procedure is called to process an argv/argc list, plus
  1176.  *    the Tk option database, in order to configure (or
  1177.  *    reconfigure) a menu widget.
  1178.  *
  1179.  * Results:
  1180.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  1181.  *    returned, then interp->result contains an error message.
  1182.  *
  1183.  * Side effects:
  1184.  *    Configuration information, such as colors, font, etc. get set
  1185.  *    for menuPtr;  old resources get freed, if there were any.
  1186.  *
  1187.  *----------------------------------------------------------------------
  1188.  */
  1189.  
  1190. static int
  1191. ConfigureMenu(interp, menuPtr, argc, argv, flags)
  1192.     Tcl_Interp *interp;        /* Used for error reporting. */
  1193.     register Menu *menuPtr;    /* Information about widget;  may or may
  1194.                  * not already have values for some fields. */
  1195.     int argc;            /* Number of valid entries in argv. */
  1196.     char **argv;        /* Arguments. */
  1197.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  1198. {
  1199.     XGCValues gcValues;
  1200.     GC newGC;
  1201.     unsigned long mask;
  1202.     int i;
  1203.     XSetWindowAttributes atts;
  1204.  
  1205.     if (Tk_ConfigureWidget(interp, menuPtr->tkwin, configSpecs,
  1206.         argc, argv, (char *) menuPtr, flags) != TCL_OK) {
  1207.     return TCL_ERROR;
  1208.     }
  1209.  
  1210.     /*
  1211.      * A few options need special processing, such as setting the
  1212.      * background from a 3-D border, or filling in complicated
  1213.      * defaults that couldn't be specified to Tk_ConfigureWidget.
  1214.      */
  1215.  
  1216.     Tk_SetBackgroundFromBorder(menuPtr->tkwin, menuPtr->border);
  1217.  
  1218.     gcValues.font = menuPtr->fontPtr->fid;
  1219.     gcValues.foreground = menuPtr->fg->pixel;
  1220.     gcValues.background = Tk_3DBorderColor(menuPtr->border)->pixel;
  1221.     newGC = Tk_GetGC(menuPtr->tkwin, GCForeground|GCBackground|GCFont,
  1222.         &gcValues);
  1223.     if (menuPtr->textGC != None) {
  1224.     Tk_FreeGC(menuPtr->display, menuPtr->textGC);
  1225.     }
  1226.     menuPtr->textGC = newGC;
  1227.  
  1228.     if (menuPtr->disabledFg != NULL) {
  1229.     gcValues.foreground = menuPtr->disabledFg->pixel;
  1230.     mask = GCForeground|GCBackground|GCFont;
  1231.     } else {
  1232.     gcValues.foreground = gcValues.background;
  1233.     if (menuPtr->gray == None) {
  1234.         menuPtr->gray = Tk_GetBitmap(interp, menuPtr->tkwin,
  1235.             Tk_GetUid("gray50"));
  1236.         if (menuPtr->gray == None) {
  1237.         return TCL_ERROR;
  1238.         }
  1239.     }
  1240.     gcValues.fill_style = FillStippled;
  1241.     gcValues.stipple = menuPtr->gray;
  1242.     mask = GCForeground|GCFillStyle|GCStipple;
  1243.     }
  1244.     newGC = Tk_GetGC(menuPtr->tkwin, mask, &gcValues);
  1245.     if (menuPtr->disabledGC != None) {
  1246.     Tk_FreeGC(menuPtr->display, menuPtr->disabledGC);
  1247.     }
  1248.     menuPtr->disabledGC = newGC;
  1249.  
  1250.     gcValues.font = menuPtr->fontPtr->fid;
  1251.     gcValues.foreground = menuPtr->activeFg->pixel;
  1252.     gcValues.background = Tk_3DBorderColor(menuPtr->activeBorder)->pixel;
  1253.     newGC = Tk_GetGC(menuPtr->tkwin, GCForeground|GCBackground|GCFont,
  1254.         &gcValues);
  1255.     if (menuPtr->activeGC != None) {
  1256.     Tk_FreeGC(menuPtr->display, menuPtr->activeGC);
  1257.     }
  1258.     menuPtr->activeGC = newGC;
  1259.  
  1260.     gcValues.foreground = menuPtr->indicatorFg->pixel;
  1261.     newGC = Tk_GetGC(menuPtr->tkwin, GCForeground|GCFont, &gcValues);
  1262.     if (menuPtr->indicatorGC != None) {
  1263.     Tk_FreeGC(menuPtr->display, menuPtr->indicatorGC);
  1264.     }
  1265.     menuPtr->indicatorGC = newGC;
  1266.  
  1267.     if (menuPtr->transient) {
  1268.     atts.override_redirect = True;
  1269.     atts.save_under = True;
  1270.     } else {
  1271.     atts.override_redirect = False;
  1272.     atts.save_under = False;
  1273.     }
  1274.     if ((atts.override_redirect
  1275.         != Tk_Attributes(menuPtr->tkwin)->override_redirect)
  1276.         || (atts.save_under
  1277.         != Tk_Attributes(menuPtr->tkwin)->save_under)) {
  1278.     Tk_ChangeWindowAttributes(menuPtr->tkwin,
  1279.         CWOverrideRedirect|CWSaveUnder, &atts);
  1280.     }
  1281.  
  1282.     /*
  1283.      * After reconfiguring a menu, we need to reconfigure all of the
  1284.      * entries in the menu, since some of the things in the children
  1285.      * (such as graphics contexts) may have to change to reflect changes
  1286.      * in the parent.
  1287.      */
  1288.  
  1289.     for (i = 0; i < menuPtr->numEntries; i++) {
  1290.     MenuEntry *mePtr;
  1291.  
  1292.     mePtr = menuPtr->entries[i];
  1293.     ConfigureMenuEntry(interp, menuPtr, mePtr, i, 0, (char **) NULL,
  1294.         TK_CONFIG_ARGV_ONLY | COMMAND_MASK << mePtr->type);
  1295.     }
  1296.  
  1297.     /*
  1298.      * Depending on the -tearOff option, make sure that there is or
  1299.      * isn't an initial tear-off entry at the beginning of the menu.
  1300.      */
  1301.  
  1302.     if (menuPtr->tearOff) {
  1303.     if ((menuPtr->numEntries == 0)
  1304.         || (menuPtr->entries[0]->type != TEAROFF_ENTRY)) {
  1305.         MenuNewEntry(menuPtr, 0, TEAROFF_ENTRY);
  1306.     }
  1307.     } else if ((menuPtr->numEntries > 0)
  1308.         && (menuPtr->entries[0]->type == TEAROFF_ENTRY)) {
  1309.     Tcl_EventuallyFree((ClientData) menuPtr->entries[0],
  1310.               DestroyMenuEntry);
  1311.     for (i = 1; i < menuPtr->numEntries;  i++) {
  1312.         menuPtr->entries[i-1] = menuPtr->entries[i];
  1313.     }
  1314.     menuPtr->numEntries--;
  1315.     }
  1316.  
  1317.     if (!(menuPtr->flags & RESIZE_PENDING)) {
  1318.     menuPtr->flags |= RESIZE_PENDING;
  1319.     Tcl_DoWhenIdle(ComputeMenuGeometry, (ClientData) menuPtr);
  1320.     }
  1321.  
  1322.     return TCL_OK;
  1323. }
  1324.  
  1325. /*
  1326.  *----------------------------------------------------------------------
  1327.  *
  1328.  * ConfigureMenuEntry --
  1329.  *
  1330.  *    This procedure is called to process an argv/argc list, plus
  1331.  *    the Tk option database, in order to configure (or
  1332.  *    reconfigure) one entry in a menu.
  1333.  *
  1334.  * Results:
  1335.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  1336.  *    returned, then interp->result contains an error message.
  1337.  *
  1338.  * Side effects:
  1339.  *    Configuration information such as label and accelerator get
  1340.  *    set for mePtr;  old resources get freed, if there were any.
  1341.  *
  1342.  *----------------------------------------------------------------------
  1343.  */
  1344.  
  1345. static int
  1346. ConfigureMenuEntry(interp, menuPtr, mePtr, index, argc, argv, flags)
  1347.     Tcl_Interp *interp;            /* Used for error reporting. */
  1348.     Menu *menuPtr;            /* Information about whole menu. */
  1349.     register MenuEntry *mePtr;        /* Information about menu entry;  may
  1350.                      * or may not already have values for
  1351.                      * some fields. */
  1352.     int index;                /* Index of mePtr within menuPtr's
  1353.                      * entries. */
  1354.     int argc;                /* Number of valid entries in argv. */
  1355.     char **argv;            /* Arguments. */
  1356.     int flags;                /* Additional flags to pass to
  1357.                      * Tk_ConfigureWidget. */
  1358. {
  1359.     XGCValues gcValues;
  1360.     GC newGC, newActiveGC, newDisabledGC;
  1361.     unsigned long mask;
  1362.     Tk_Image image;
  1363.  
  1364.     /*
  1365.      * If this entry is a cascade and the cascade is posted, then unpost
  1366.      * it before reconfiguring the entry (otherwise the reconfigure might
  1367.      * change the name of the cascaded entry, leaving a posted menu
  1368.      * high and dry).
  1369.      */
  1370.  
  1371.     if (menuPtr->postedCascade == mePtr) {
  1372.     if (PostSubmenu(menuPtr->interp, menuPtr, (MenuEntry *) NULL)
  1373.         != TCL_OK) {
  1374.         Tcl_BackgroundError(menuPtr->interp);
  1375.     }
  1376.     }
  1377.  
  1378.     /*
  1379.      * If this entry is a check button or radio button, then remove
  1380.      * its old trace procedure.
  1381.      */
  1382.  
  1383.     if ((mePtr->name != NULL) &&
  1384.         ((mePtr->type == CHECK_BUTTON_ENTRY)
  1385.         || (mePtr->type == RADIO_BUTTON_ENTRY))) {
  1386.     Tcl_UntraceVar(menuPtr->interp, mePtr->name,
  1387.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  1388.         MenuVarProc, (ClientData) mePtr);
  1389.     }
  1390.  
  1391. #ifdef STk_CODE
  1392.     if (Tk_Menu_ConfigureWidget(interp, &menuPtr->entries[index], menuPtr->tkwin, 
  1393.                 entryConfigSpecs,
  1394. #else
  1395.     if (Tk_ConfigureWidget(interp, menuPtr->tkwin, entryConfigSpecs,
  1396. #endif
  1397.         argc, argv, (char *) mePtr,
  1398.         flags | (COMMAND_MASK << mePtr->type)) != TCL_OK) {
  1399.     return TCL_ERROR;
  1400.     }
  1401.  
  1402.     /*
  1403.      * The code below handles special configuration stuff not taken
  1404.      * care of by Tk_ConfigureWidget, such as special processing for
  1405.      * defaults, sizing strings, graphics contexts, etc.
  1406.      */
  1407.  
  1408.     if (mePtr->label == NULL) {
  1409.     mePtr->labelLength = 0;
  1410.     } else {
  1411.     mePtr->labelLength = strlen(mePtr->label);
  1412.     }
  1413.     if (mePtr->accel == NULL) {
  1414.     mePtr->accelLength = 0;
  1415.     } else {
  1416.     mePtr->accelLength = strlen(mePtr->accel);
  1417.     }
  1418.  
  1419.     if (mePtr->state == tkActiveUid) {
  1420.     if (index != menuPtr->active) {
  1421.         ActivateMenuEntry(menuPtr, index);
  1422.     }
  1423.     } else {
  1424.     if (index == menuPtr->active) {
  1425.         ActivateMenuEntry(menuPtr, -1);
  1426.     }
  1427.     if ((mePtr->state != tkNormalUid) && (mePtr->state != tkDisabledUid)) {
  1428.         Tcl_AppendResult(interp, "bad state value \"", mePtr->state,
  1429.             "\": must be normal, active, or disabled", (char *) NULL);
  1430.         mePtr->state = tkNormalUid;
  1431.         return TCL_ERROR;
  1432.     }
  1433.     }
  1434.  
  1435.     if ((mePtr->fontPtr != NULL) || (mePtr->border != NULL)
  1436.         || (mePtr->fg != NULL) || (mePtr->activeBorder != NULL)
  1437.         || (mePtr->activeFg != NULL)) {
  1438.     gcValues.foreground = (mePtr->fg != NULL) ? mePtr->fg->pixel
  1439.         : menuPtr->fg->pixel;
  1440.     gcValues.background = Tk_3DBorderColor(
  1441.         (mePtr->border != NULL) ? mePtr->border : menuPtr->border)
  1442.         ->pixel;
  1443.     gcValues.font = (mePtr->fontPtr != NULL) ? mePtr->fontPtr->fid
  1444.         : menuPtr->fontPtr->fid;
  1445.  
  1446.     /*
  1447.      * Note: disable GraphicsExpose events;  we know there won't be
  1448.      * obscured areas when copying from an off-screen pixmap to the
  1449.      * screen and this gets rid of unnecessary events.
  1450.      */
  1451.  
  1452.     gcValues.graphics_exposures = False;
  1453.     newGC = Tk_GetGC(menuPtr->tkwin,
  1454.         GCForeground|GCBackground|GCFont|GCGraphicsExposures,
  1455.         &gcValues);
  1456.  
  1457.     if (menuPtr->disabledFg != NULL) {
  1458.         gcValues.foreground = menuPtr->disabledFg->pixel;
  1459.         mask = GCForeground|GCBackground|GCFont|GCGraphicsExposures;
  1460.     } else {
  1461.         gcValues.foreground = gcValues.background;
  1462.         gcValues.fill_style = FillStippled;
  1463.         gcValues.stipple = menuPtr->gray;
  1464.         mask = GCForeground|GCFillStyle|GCStipple;
  1465.     }
  1466.     newDisabledGC = Tk_GetGC(menuPtr->tkwin, mask, &gcValues);
  1467.  
  1468.     gcValues.foreground = (mePtr->activeFg != NULL)
  1469.         ? mePtr->activeFg->pixel : menuPtr->activeFg->pixel;
  1470.     gcValues.background = Tk_3DBorderColor(
  1471.         (mePtr->activeBorder != NULL) ? mePtr->activeBorder
  1472.         : menuPtr->activeBorder)->pixel;
  1473.     newActiveGC = Tk_GetGC(menuPtr->tkwin,
  1474.         GCForeground|GCBackground|GCFont|GCGraphicsExposures,
  1475.         &gcValues);
  1476.     } else {
  1477.     newGC = None;
  1478.     newActiveGC = None;
  1479.     newDisabledGC = None;
  1480.     }
  1481.     if (mePtr->textGC != None) {
  1482.         Tk_FreeGC(menuPtr->display, mePtr->textGC);
  1483.     }
  1484.     mePtr->textGC = newGC;
  1485.     if (mePtr->activeGC != None) {
  1486.         Tk_FreeGC(menuPtr->display, mePtr->activeGC);
  1487.     }
  1488.     mePtr->activeGC = newActiveGC;
  1489.     if (mePtr->disabledGC != None) {
  1490.         Tk_FreeGC(menuPtr->display, mePtr->disabledGC);
  1491.     }
  1492.     mePtr->disabledGC = newDisabledGC;
  1493.     if (mePtr->indicatorFg != NULL) {
  1494.     gcValues.foreground = mePtr->indicatorFg->pixel;
  1495.     newGC = Tk_GetGC(menuPtr->tkwin, GCForeground, &gcValues);
  1496.     } else {
  1497.     newGC = None;
  1498.     }
  1499.     if (mePtr->indicatorGC != None) {
  1500.     Tk_FreeGC(menuPtr->display, mePtr->indicatorGC);
  1501.     }
  1502.     mePtr->indicatorGC = newGC;
  1503.  
  1504.     if ((mePtr->type == CHECK_BUTTON_ENTRY)
  1505.         || (mePtr->type == RADIO_BUTTON_ENTRY)) {
  1506.     char *value;
  1507.  
  1508.     if (mePtr->name == NULL) {
  1509.         mePtr->name = (char *) ckalloc((unsigned) (mePtr->labelLength + 1));
  1510.         strcpy(mePtr->name, (mePtr->label == NULL) ? "" : mePtr->label);
  1511.     }
  1512.     if (mePtr->onValue == NULL) {
  1513.         mePtr->onValue = (char *) ckalloc((unsigned)
  1514.             (mePtr->labelLength + 1));
  1515.         strcpy(mePtr->onValue, (mePtr->label == NULL) ? "" : mePtr->label);
  1516.     }
  1517.  
  1518.     /*
  1519.      * Select the entry if the associated variable has the
  1520.      * appropriate value, initialize the variable if it doesn't
  1521.      * exist, then set a trace on the variable to monitor future
  1522.      * changes to its value.
  1523.      */
  1524.  
  1525.     value = Tcl_GetVar(interp, mePtr->name, TCL_GLOBAL_ONLY);
  1526.     mePtr->flags &= ~ENTRY_SELECTED;
  1527.     if (value != NULL) {
  1528.         if (strcmp(value, mePtr->onValue) == 0) {
  1529.         mePtr->flags |= ENTRY_SELECTED;
  1530.         }
  1531.     } else {
  1532.         Tcl_SetVar(interp, mePtr->name,
  1533. #ifdef STk_CODE
  1534.             (mePtr->type == CHECK_BUTTON_ENTRY) ? mePtr->offValue : "#f",
  1535. #else
  1536.             (mePtr->type == CHECK_BUTTON_ENTRY) ? mePtr->offValue : "",
  1537. #endif
  1538.             TCL_GLOBAL_ONLY);
  1539.     }
  1540.     Tcl_TraceVar(interp, mePtr->name,
  1541.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  1542.         MenuVarProc, (ClientData) mePtr);
  1543.     }
  1544.  
  1545.     /*
  1546.      * Get the images for the entry, if there are any.  Allocate the
  1547.      * new images before freeing the old ones, so that the reference
  1548.      * counts don't go to zero and cause image data to be discarded.
  1549.      */
  1550.  
  1551.     if (mePtr->imageString != NULL) {
  1552.     image = Tk_GetImage(interp, menuPtr->tkwin, mePtr->imageString,
  1553.         MenuImageProc, (ClientData) mePtr);
  1554.     if (image == NULL) {
  1555.         return TCL_ERROR;
  1556.     }
  1557.     } else {
  1558.     image = NULL;
  1559.     }
  1560.     if (mePtr->image != NULL) {
  1561.     Tk_FreeImage(mePtr->image);
  1562.     }
  1563.     mePtr->image = image;
  1564.     if (mePtr->selectImageString != NULL) {
  1565.     image = Tk_GetImage(interp, menuPtr->tkwin, mePtr->selectImageString,
  1566.         MenuSelectImageProc, (ClientData) mePtr);
  1567.     if (image == NULL) {
  1568.         return TCL_ERROR;
  1569.     }
  1570.     } else {
  1571.     image = NULL;
  1572.     }
  1573.     if (mePtr->selectImage != NULL) {
  1574.     Tk_FreeImage(mePtr->selectImage);
  1575.     }
  1576.     mePtr->selectImage = image;
  1577.  
  1578.     if (!(menuPtr->flags & RESIZE_PENDING)) {
  1579.     menuPtr->flags |= RESIZE_PENDING;
  1580.     Tcl_DoWhenIdle(ComputeMenuGeometry, (ClientData) menuPtr);
  1581.     }
  1582.     return TCL_OK;
  1583. }
  1584.  
  1585. /*
  1586.  *--------------------------------------------------------------
  1587.  *
  1588.  * ComputeMenuGeometry --
  1589.  *
  1590.  *    This procedure is invoked to recompute the size and
  1591.  *    layout of a menu.  It is called as a when-idle handler so
  1592.  *    that it only gets done once, even if a group of changes is
  1593.  *    made to the menu.
  1594.  *
  1595.  * Results:
  1596.  *    None.
  1597.  *
  1598.  * Side effects:
  1599.  *    Fields of menu entries are changed to reflect their
  1600.  *    current positions, and the size of the menu window
  1601.  *    itself may be changed.
  1602.  *
  1603.  *--------------------------------------------------------------
  1604.  */
  1605.  
  1606. static void
  1607. ComputeMenuGeometry(clientData)
  1608.     ClientData clientData;        /* Structure describing menu. */
  1609. {
  1610.     Menu *menuPtr = (Menu *) clientData;
  1611.     register MenuEntry *mePtr;
  1612.     XFontStruct *fontPtr;
  1613.     int maxLabelWidth, maxIndicatorWidth, maxAccelWidth;
  1614.     int width, height, indicatorSpace;
  1615.     int i, y;
  1616.     int imageWidth, imageHeight;
  1617.  
  1618.     if (menuPtr->tkwin == NULL) {
  1619.     return;
  1620.     }
  1621.  
  1622.     maxLabelWidth = maxIndicatorWidth = maxAccelWidth = 0;
  1623.     y = menuPtr->borderWidth;
  1624.  
  1625.     for (i = 0; i < menuPtr->numEntries; i++) {
  1626.     mePtr = menuPtr->entries[i];
  1627.     indicatorSpace = 0;
  1628.     fontPtr = mePtr->fontPtr;
  1629.     if (fontPtr == NULL) {
  1630.         fontPtr = menuPtr->fontPtr;
  1631.     }
  1632.  
  1633.     /*
  1634.      * For each entry, compute the height required by that
  1635.      * particular entry, plus three widths:  the width of the
  1636.      * label, the width to allow for an indicator to be displayed
  1637.      * to the left of the label (if any), and the width of the
  1638.      * accelerator to be displayed to the right of the label
  1639.      * (if any).  These sizes depend, of course, on the type
  1640.      * of the entry.
  1641.      */
  1642.  
  1643.     if (mePtr->image != NULL) {
  1644.         Tk_SizeOfImage(mePtr->image, &imageWidth, &imageHeight);
  1645.  
  1646.         imageOrBitmap:
  1647.         mePtr->height = imageHeight;
  1648.         width = imageWidth;
  1649.         if (mePtr->indicatorOn) {
  1650.         if (mePtr->type == CHECK_BUTTON_ENTRY) {
  1651.             indicatorSpace = (14*mePtr->height)/10;
  1652.             mePtr->indicatorDiameter = (65*mePtr->height)/100;
  1653.         } else if (mePtr->type == RADIO_BUTTON_ENTRY) {
  1654.             indicatorSpace = (14*mePtr->height)/10;
  1655.             mePtr->indicatorDiameter = (75*mePtr->height)/100;
  1656.         }
  1657.         }
  1658.     } else if (mePtr->bitmap != None) {
  1659.         Tk_SizeOfBitmap(menuPtr->display, mePtr->bitmap,
  1660.             &imageWidth, &imageHeight);
  1661.         goto imageOrBitmap;
  1662.     } else {
  1663.         mePtr->height = fontPtr->ascent + fontPtr->descent;
  1664.         if (mePtr->label != NULL) {
  1665.         (void) TkMeasureChars(fontPtr, mePtr->label,
  1666.             mePtr->labelLength, 0, (int) 100000, 0,
  1667.             TK_NEWLINES_NOT_SPECIAL, &width);
  1668.         } else {
  1669.         width = 0;
  1670.         }
  1671.         if (mePtr->indicatorOn) {
  1672.         if (mePtr->type == CHECK_BUTTON_ENTRY) {
  1673.             indicatorSpace = mePtr->height;
  1674.             mePtr->indicatorDiameter = (80*mePtr->height)/100;
  1675.         } else if (mePtr->type == RADIO_BUTTON_ENTRY) {
  1676.             indicatorSpace = mePtr->height;
  1677.             mePtr->indicatorDiameter = mePtr->height;
  1678.         }
  1679.         }
  1680.     }
  1681.     mePtr->height += 2*menuPtr->activeBorderWidth + 2;
  1682.     if (width > maxLabelWidth) {
  1683.         maxLabelWidth = width;
  1684.     }
  1685.     if (mePtr->type == CASCADE_ENTRY) {
  1686.         width = 2*CASCADE_ARROW_WIDTH;
  1687.     } else if (mePtr->accel != NULL) {
  1688.         (void) TkMeasureChars(fontPtr, mePtr->accel, mePtr->accelLength,
  1689.             0, (int) 100000, 0, TK_NEWLINES_NOT_SPECIAL, &width);
  1690.     } else {
  1691.         width = 0;
  1692.     }
  1693.     if (width > maxAccelWidth) {
  1694.         maxAccelWidth = width;
  1695.     }
  1696.     if (mePtr->type == SEPARATOR_ENTRY) {
  1697.         mePtr->height = 8;
  1698.     }
  1699.     if (mePtr->type == TEAROFF_ENTRY) {
  1700.         mePtr->height = 12;
  1701.     }
  1702.     if (indicatorSpace > maxIndicatorWidth) {
  1703.         maxIndicatorWidth = indicatorSpace;
  1704.     }
  1705.     mePtr->y = y;
  1706.     y += mePtr->height;
  1707.     }
  1708.  
  1709.     /*
  1710.      * Got all the sizes.  Update fields in the menu structure, then
  1711.      * resize the window if necessary.  Leave margins on either side
  1712.      * of the indicator (or just one margin if there is no indicator).
  1713.      * Leave another margin on the right side of the label, plus yet
  1714.      * another margin to the right of the accelerator (if there is one).
  1715.      */
  1716.  
  1717.     menuPtr->indicatorSpace = maxIndicatorWidth + MARGIN_WIDTH;
  1718.     if (maxIndicatorWidth != 0) {
  1719.     menuPtr->indicatorSpace += MARGIN_WIDTH;
  1720.     }
  1721.     menuPtr->labelWidth = maxLabelWidth + MARGIN_WIDTH;
  1722.     width = menuPtr->indicatorSpace + menuPtr->labelWidth + maxAccelWidth
  1723.         + 2*menuPtr->borderWidth + 2*menuPtr->activeBorderWidth;
  1724.     if (maxAccelWidth != 0) {
  1725.     width += MARGIN_WIDTH;
  1726.     }
  1727.     height = y + menuPtr->borderWidth;
  1728.  
  1729.     /*
  1730.      * The X server doesn't like zero dimensions, so round up to at least
  1731.      * 1 (a zero-sized menu should never really occur, anyway).
  1732.      */
  1733.  
  1734.     if (width <= 0) {
  1735.     width = 1;
  1736.     }
  1737.     if (height <= 0) {
  1738.     height = 1;
  1739.     }
  1740.     if ((width != Tk_ReqWidth(menuPtr->tkwin)) ||
  1741.         (height != Tk_ReqHeight(menuPtr->tkwin))) {
  1742.     Tk_GeometryRequest(menuPtr->tkwin, width, height);
  1743.     } else {
  1744.     /*
  1745.      * Must always force a redisplay here if the window is mapped
  1746.      * (even if the size didn't change, something else might have
  1747.      * changed in the menu, such as a label or accelerator).  The
  1748.      * resize will force a redisplay above.
  1749.      */
  1750.  
  1751.     EventuallyRedrawMenu(menuPtr, (MenuEntry *) NULL);
  1752.     }
  1753.  
  1754.     menuPtr->flags &= ~RESIZE_PENDING;
  1755. }
  1756.  
  1757. /*
  1758.  *----------------------------------------------------------------------
  1759.  *
  1760.  * DisplayMenu --
  1761.  *
  1762.  *    This procedure is invoked to display a menu widget.
  1763.  *
  1764.  * Results:
  1765.  *    None.
  1766.  *
  1767.  * Side effects:
  1768.  *    Commands are output to X to display the menu in its
  1769.  *    current mode.
  1770.  *
  1771.  *----------------------------------------------------------------------
  1772.  */
  1773.  
  1774. static void
  1775. DisplayMenu(clientData)
  1776.     ClientData clientData;    /* Information about widget. */
  1777. {
  1778.     register Menu *menuPtr = (Menu *) clientData;
  1779.     register MenuEntry *mePtr;
  1780.     register Tk_Window tkwin = menuPtr->tkwin;
  1781.     Tk_3DBorder bgBorder, activeBorder;
  1782.     XFontStruct *fontPtr;
  1783.     int index, baseline, strictMotif, leftEdge, y,  height;
  1784.     GC gc;
  1785.     XPoint points[3];
  1786.  
  1787.     menuPtr->flags &= ~REDRAW_PENDING;
  1788.     if ((menuPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
  1789.     return;
  1790.     }
  1791.  
  1792.     /*
  1793.      * Loop through all of the entries, drawing them one at a time.
  1794.      */
  1795.  
  1796.     strictMotif = Tk_StrictMotif(menuPtr->tkwin);
  1797.     leftEdge = menuPtr->borderWidth + menuPtr->indicatorSpace
  1798.         + menuPtr->activeBorderWidth;
  1799.     for (index = 0; index < menuPtr->numEntries; index++) {
  1800.     mePtr = menuPtr->entries[index];
  1801.     if (!(mePtr->flags & ENTRY_NEEDS_REDISPLAY)) {
  1802.         continue;
  1803.     }
  1804.     mePtr->flags &= ~ENTRY_NEEDS_REDISPLAY;
  1805.  
  1806.     /*
  1807.      * Background.
  1808.      */
  1809.  
  1810.     bgBorder = mePtr->border;
  1811.     if (bgBorder == NULL) {
  1812.         bgBorder = menuPtr->border;
  1813.     }
  1814.     if (strictMotif) {
  1815.         activeBorder = bgBorder;
  1816.     } else {
  1817.         activeBorder = mePtr->activeBorder;
  1818.         if (activeBorder == NULL) {
  1819.         activeBorder = menuPtr->activeBorder;
  1820.         }
  1821.     }
  1822.     if (mePtr->state == tkActiveUid) {
  1823.         bgBorder = activeBorder;
  1824.         Tk_Fill3DRectangle(menuPtr->tkwin, Tk_WindowId(tkwin),
  1825.             bgBorder, menuPtr->borderWidth, mePtr->y,
  1826.             Tk_Width(tkwin) - 2*menuPtr->borderWidth, mePtr->height,
  1827.             menuPtr->activeBorderWidth, TK_RELIEF_RAISED);
  1828.     } else {
  1829.         Tk_Fill3DRectangle(menuPtr->tkwin, Tk_WindowId(tkwin),
  1830.             bgBorder, menuPtr->borderWidth, mePtr->y,
  1831.             Tk_Width(tkwin) - 2*menuPtr->borderWidth, mePtr->height,
  1832.             0, TK_RELIEF_FLAT);
  1833.     }
  1834.  
  1835.     /*
  1836.      * Choose the gc for drawing the foreground part of the entry.
  1837.      */
  1838.  
  1839.     if ((mePtr->state == tkActiveUid) && !strictMotif) {
  1840.         gc = mePtr->activeGC;
  1841.         if (gc == NULL) {
  1842.         gc = menuPtr->activeGC;
  1843.         }
  1844.     } else {
  1845.         if ((mePtr->state == tkDisabledUid)
  1846.             && (menuPtr->disabledFg != NULL)) {
  1847.         gc = mePtr->disabledGC;
  1848.         if (gc == NULL) {
  1849.             gc = menuPtr->disabledGC;
  1850.         }
  1851.         } else {
  1852.         gc = mePtr->textGC;
  1853.         if (gc == NULL) {
  1854.             gc = menuPtr->textGC;
  1855.         }
  1856.         }
  1857.     }
  1858.  
  1859.     /*
  1860.      * Draw label or bitmap or image for entry.
  1861.      */
  1862.  
  1863.     fontPtr = mePtr->fontPtr;
  1864.     if (fontPtr == NULL) {
  1865.         fontPtr = menuPtr->fontPtr;
  1866.     }
  1867.     baseline = mePtr->y + (mePtr->height + fontPtr->ascent
  1868.         - fontPtr->descent)/2;
  1869.     if (mePtr->image != NULL) {
  1870.         int width, height;
  1871.  
  1872.         Tk_SizeOfImage(mePtr->image, &width, &height);
  1873.         if ((mePtr->selectImage != NULL)
  1874.             && (mePtr->flags & ENTRY_SELECTED)) {
  1875.         Tk_RedrawImage(mePtr->selectImage, 0, 0, width, height,
  1876.             Tk_WindowId(tkwin), leftEdge,
  1877.             (int) (mePtr->y + (mePtr->height - height)/2));
  1878.         } else {
  1879.         Tk_RedrawImage(mePtr->image, 0, 0, width, height,
  1880.             Tk_WindowId(tkwin), leftEdge,
  1881.             (int) (mePtr->y + (mePtr->height - height)/2));
  1882.         }
  1883.     } else if (mePtr->bitmap != None) {
  1884.         int width, height;
  1885.  
  1886.         Tk_SizeOfBitmap(menuPtr->display, mePtr->bitmap, &width, &height);
  1887.         XCopyPlane(menuPtr->display, mePtr->bitmap, Tk_WindowId(tkwin),
  1888.             gc, 0, 0, (unsigned) width, (unsigned) height, leftEdge,
  1889.             (int) (mePtr->y + (mePtr->height - height)/2), 1);
  1890.     } else {
  1891.         baseline = mePtr->y + (mePtr->height + fontPtr->ascent
  1892.             - fontPtr->descent)/2;
  1893.         if (mePtr->label != NULL) {
  1894.         TkDisplayChars(menuPtr->display, Tk_WindowId(tkwin), gc,
  1895.             fontPtr, mePtr->label, mePtr->labelLength,
  1896.             leftEdge, baseline, leftEdge,
  1897.             TK_NEWLINES_NOT_SPECIAL);
  1898.         if (mePtr->underline >= 0) {
  1899.             TkUnderlineChars(menuPtr->display, Tk_WindowId(tkwin), gc,
  1900.                 fontPtr, mePtr->label, leftEdge, baseline,
  1901.                 leftEdge, TK_NEWLINES_NOT_SPECIAL,
  1902.                 mePtr->underline, mePtr->underline);
  1903.         }
  1904.         }
  1905.     }
  1906.  
  1907.     /*
  1908.      * Draw accelerator or cascade arrow.
  1909.      */
  1910.  
  1911.     if (mePtr->type == CASCADE_ENTRY) {
  1912.         points[0].x = Tk_Width(tkwin) - menuPtr->borderWidth
  1913.             - menuPtr->activeBorderWidth - MARGIN_WIDTH
  1914.             - CASCADE_ARROW_WIDTH;
  1915.         points[0].y = mePtr->y + (mePtr->height - CASCADE_ARROW_HEIGHT)/2;
  1916.         points[1].x = points[0].x;
  1917.         points[1].y = points[0].y + CASCADE_ARROW_HEIGHT;
  1918.         points[2].x = points[0].x + CASCADE_ARROW_WIDTH;
  1919.         points[2].y = points[0].y + CASCADE_ARROW_HEIGHT/2;
  1920.         Tk_Fill3DPolygon(menuPtr->tkwin, Tk_WindowId(tkwin), activeBorder,
  1921.             points, 3, DECORATION_BORDER_WIDTH,
  1922.             (menuPtr->postedCascade == mePtr) ? TK_RELIEF_SUNKEN
  1923.             : TK_RELIEF_RAISED);
  1924.     } else if (mePtr->accel != NULL) {
  1925.         TkDisplayChars(menuPtr->display, Tk_WindowId(tkwin), gc,
  1926.             fontPtr, mePtr->accel, mePtr->accelLength,
  1927.             leftEdge + menuPtr->labelWidth, baseline,
  1928.             leftEdge + menuPtr->labelWidth, TK_NEWLINES_NOT_SPECIAL);
  1929.     }
  1930.  
  1931.     /*
  1932.      * Draw check-button indicator.
  1933.      */
  1934.  
  1935.     gc = mePtr->indicatorGC;
  1936.     if (gc == None) {
  1937.         gc = menuPtr->indicatorGC;
  1938.     }
  1939.     if ((mePtr->type == CHECK_BUTTON_ENTRY) && mePtr->indicatorOn) {
  1940.         int dim, x, y;
  1941.  
  1942.         dim = mePtr->indicatorDiameter;
  1943.         x = menuPtr->borderWidth + menuPtr->activeBorderWidth
  1944.             + (menuPtr->indicatorSpace - dim)/2;
  1945.         y = mePtr->y + (mePtr->height - dim)/2;
  1946.         Tk_Fill3DRectangle(menuPtr->tkwin, Tk_WindowId(tkwin),
  1947.             menuPtr->border, x, y, dim, dim,
  1948.             DECORATION_BORDER_WIDTH, TK_RELIEF_SUNKEN);
  1949.         x += DECORATION_BORDER_WIDTH;
  1950.         y += DECORATION_BORDER_WIDTH;
  1951.         dim -= 2*DECORATION_BORDER_WIDTH;
  1952.         if ((dim > 0) && (mePtr->flags & ENTRY_SELECTED)) {
  1953.         XFillRectangle(menuPtr->display, Tk_WindowId(tkwin), gc,
  1954.             x, y, (unsigned int) dim, (unsigned int) dim);
  1955.         }
  1956.     }
  1957.  
  1958.     /*
  1959.      * Draw radio-button indicator.
  1960.      */
  1961.  
  1962.     if ((mePtr->type == RADIO_BUTTON_ENTRY) && mePtr->indicatorOn) {
  1963.         XPoint points[4];
  1964.         int radius;
  1965.  
  1966.         radius = mePtr->indicatorDiameter/2;
  1967.         points[0].x = menuPtr->borderWidth + menuPtr->activeBorderWidth
  1968.             + (menuPtr->indicatorSpace - mePtr->indicatorDiameter)/2;
  1969.         points[0].y = mePtr->y + (mePtr->height)/2;
  1970.         points[1].x = points[0].x + radius;
  1971.         points[1].y = points[0].y + radius;
  1972.         points[2].x = points[1].x + radius;
  1973.         points[2].y = points[0].y;
  1974.         points[3].x = points[1].x;
  1975.         points[3].y = points[0].y - radius;
  1976.         if (mePtr->flags & ENTRY_SELECTED) {
  1977.         XFillPolygon(menuPtr->display, Tk_WindowId(tkwin), gc,
  1978.             points, 4, Convex, CoordModeOrigin);
  1979.         } else {
  1980.         Tk_Fill3DPolygon(menuPtr->tkwin, Tk_WindowId(tkwin),
  1981.             menuPtr->border, points, 4, DECORATION_BORDER_WIDTH,
  1982.             TK_RELIEF_FLAT);
  1983.         }
  1984.         Tk_Draw3DPolygon(menuPtr->tkwin, Tk_WindowId(tkwin),
  1985.             menuPtr->border, points, 4, DECORATION_BORDER_WIDTH,
  1986.             TK_RELIEF_SUNKEN);
  1987.     }
  1988.  
  1989.     /*
  1990.      * Draw separator.
  1991.      */
  1992.  
  1993.     if (mePtr->type == SEPARATOR_ENTRY) {
  1994.         XPoint points[2];
  1995.         int margin;
  1996.  
  1997.         margin = (fontPtr->ascent + fontPtr->descent)/2;
  1998.         points[0].x = 0;
  1999.         points[0].y = mePtr->y + mePtr->height/2;
  2000.         points[1].x = Tk_Width(tkwin) - 1;
  2001.         points[1].y = points[0].y;
  2002.         Tk_Draw3DPolygon(menuPtr->tkwin, Tk_WindowId(tkwin),
  2003.             menuPtr->border, points, 2, 1, TK_RELIEF_RAISED);
  2004.     }
  2005.  
  2006.     /*
  2007.      * Draw tear-off line.
  2008.      */
  2009.  
  2010.     if (mePtr->type == TEAROFF_ENTRY) {
  2011.         XPoint points[2];
  2012.         int margin, width, maxX;
  2013.  
  2014.         margin = (fontPtr->ascent + fontPtr->descent)/2;
  2015.         points[0].x = 0;
  2016.         points[0].y = mePtr->y + mePtr->height/2;
  2017.         points[1].y = points[0].y;
  2018.         width = 6;
  2019.         maxX  = Tk_Width(tkwin) - 1;
  2020.  
  2021.         while (points[0].x < maxX) {
  2022.         points[1].x = points[0].x + width;
  2023.         if (points[1].x > maxX) {
  2024.             points[1].x = maxX;
  2025.         }
  2026.         Tk_Draw3DPolygon(menuPtr->tkwin, Tk_WindowId(tkwin),
  2027.             menuPtr->border, points, 2, 1, TK_RELIEF_RAISED);
  2028.         points[0].x += 2*width;
  2029.         }
  2030.     }
  2031.  
  2032.     /*
  2033.      * If the entry is disabled with a stipple rather than a special
  2034.      * foreground color, generate the stippled effect.
  2035.      */
  2036.  
  2037.     if ((mePtr->state == tkDisabledUid) && (menuPtr->disabledFg == NULL)) {
  2038.         XFillRectangle(menuPtr->display, Tk_WindowId(tkwin),
  2039.             menuPtr->disabledGC, menuPtr->borderWidth,
  2040.             mePtr->y,
  2041.             (unsigned) (Tk_Width(tkwin) - 2*menuPtr->borderWidth),
  2042.             (unsigned) mePtr->height);
  2043.     }
  2044.     }
  2045.  
  2046.     /*
  2047.      * If there is extra space after the last entry in the menu,
  2048.      * clear it.
  2049.      */
  2050.  
  2051.     if (menuPtr->numEntries >= 1) {
  2052.     mePtr = menuPtr->entries[menuPtr->numEntries-1];
  2053.     y = mePtr->y + mePtr->height;
  2054.     } else {
  2055.     y = menuPtr->borderWidth;
  2056.     }
  2057.     height = Tk_Height(tkwin) - menuPtr->borderWidth - y;
  2058.     if (height > 0) {
  2059.     Tk_Fill3DRectangle(tkwin, Tk_WindowId(tkwin),
  2060.         menuPtr->border, menuPtr->borderWidth, y, 
  2061.         Tk_Width(tkwin) - 2*menuPtr->borderWidth,
  2062.         height, 0, TK_RELIEF_FLAT);
  2063.     }
  2064.  
  2065.     Tk_Draw3DRectangle(menuPtr->tkwin, Tk_WindowId(tkwin),
  2066.         menuPtr->border, 0, 0, Tk_Width(tkwin), Tk_Height(tkwin),
  2067.         menuPtr->borderWidth, menuPtr->relief);
  2068. }
  2069.  
  2070. /*
  2071.  *--------------------------------------------------------------
  2072.  *
  2073.  * GetMenuIndex --
  2074.  *
  2075.  *    Parse a textual index into a menu and return the numerical
  2076.  *    index of the indicated entry.
  2077.  *
  2078.  * Results:
  2079.  *    A standard Tcl result.  If all went well, then *indexPtr is
  2080.  *    filled in with the entry index corresponding to string
  2081.  *    (ranges from -1 to the number of entries in the menu minus
  2082.  *    one).  Otherwise an error message is left in interp->result.
  2083.  *
  2084.  * Side effects:
  2085.  *    None.
  2086.  *
  2087.  *--------------------------------------------------------------
  2088.  */
  2089.  
  2090. static int
  2091. GetMenuIndex(interp, menuPtr, string, lastOK, indexPtr)
  2092.     Tcl_Interp *interp;        /* For error messages. */
  2093.     Menu *menuPtr;        /* Menu for which the index is being
  2094.                  * specified. */
  2095.     char *string;        /* Specification of an entry in menu.  See
  2096.                  * manual entry for valid .*/
  2097.     int lastOK;            /* Non-zero means its OK to return index
  2098.                  * just *after* last entry. */
  2099.     int *indexPtr;        /* Where to store converted relief. */
  2100. {
  2101.     int i, y;
  2102.  
  2103.     if ((string[0] == 'a') && (strcmp(string, "active") == 0)) {
  2104.     *indexPtr = menuPtr->active;
  2105.     return TCL_OK;
  2106.     }
  2107.  
  2108.     if (((string[0] == 'l') && (strcmp(string, "last") == 0))
  2109.         || ((string[0] == 'e') && (strcmp(string, "end") == 0))) {
  2110.     *indexPtr = menuPtr->numEntries - ((lastOK) ? 0 : 1);
  2111.     return TCL_OK;
  2112.     }
  2113.  
  2114.     if ((string[0] == 'n') && (strcmp(string, "none") == 0)) {
  2115.     *indexPtr = -1;
  2116.     return TCL_OK;
  2117.     }
  2118.  
  2119.     if (string[0] == '@') {
  2120.     if (Tcl_GetInt(interp, string+1,  &y) == TCL_OK) {
  2121.         for (i = 0; i < menuPtr->numEntries; i++) {
  2122.         MenuEntry *mePtr = menuPtr->entries[i];
  2123.  
  2124.         if (y < (mePtr->y + mePtr->height)) {
  2125.             break;
  2126.         }
  2127.         }
  2128.         if (i >= menuPtr->numEntries) {
  2129.         i = menuPtr->numEntries-1;
  2130.         }
  2131.         *indexPtr = i;
  2132.         return TCL_OK;
  2133.     } else {
  2134.         Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  2135.     }
  2136.     }
  2137.  
  2138.     if (isdigit(UCHAR(string[0]))) {
  2139.     if (Tcl_GetInt(interp, string,  &i) == TCL_OK) {
  2140.         if (i >= menuPtr->numEntries) {
  2141.         if (lastOK) {
  2142.             i = menuPtr->numEntries;
  2143.         } else {
  2144.             i = menuPtr->numEntries-1;
  2145.         }
  2146.         } else if (i < 0) {
  2147.         i = -1;
  2148.         }
  2149.         *indexPtr = i;
  2150.         return TCL_OK;
  2151.     }
  2152.     Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  2153.     }
  2154.  
  2155.     for (i = 0; i < menuPtr->numEntries; i++) {
  2156.     char *label;
  2157.  
  2158.     label = menuPtr->entries[i]->label;
  2159.     if ((label != NULL)
  2160.         && (Tcl_StringMatch(menuPtr->entries[i]->label, string))) {
  2161.         *indexPtr = i;
  2162.         return TCL_OK;
  2163.     }
  2164.     }
  2165.  
  2166.     Tcl_AppendResult(interp, "bad menu entry index \"",
  2167.         string, "\"", (char *) NULL);
  2168.     return TCL_ERROR;
  2169. }
  2170.  
  2171. /*
  2172.  *--------------------------------------------------------------
  2173.  *
  2174.  * MenuEventProc --
  2175.  *
  2176.  *    This procedure is invoked by the Tk dispatcher for various
  2177.  *    events on menus.
  2178.  *
  2179.  * Results:
  2180.  *    None.
  2181.  *
  2182.  * Side effects:
  2183.  *    When the window gets deleted, internal structures get
  2184.  *    cleaned up.  When it gets exposed, it is redisplayed.
  2185.  *
  2186.  *--------------------------------------------------------------
  2187.  */
  2188.  
  2189. static void
  2190. MenuEventProc(clientData, eventPtr)
  2191.     ClientData clientData;    /* Information about window. */
  2192.     XEvent *eventPtr;        /* Information about event. */
  2193. {
  2194.     Menu *menuPtr = (Menu *) clientData;
  2195.     if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
  2196.     EventuallyRedrawMenu(menuPtr, (MenuEntry *) NULL);
  2197.     } else if (eventPtr->type == ConfigureNotify) {
  2198.     EventuallyRedrawMenu(menuPtr, (MenuEntry *) NULL);
  2199.     } else if (eventPtr->type == DestroyNotify) {
  2200.     if (menuPtr->tkwin != NULL) {
  2201.         menuPtr->tkwin = NULL;
  2202.         Tcl_DeleteCommand(menuPtr->interp,
  2203.             Tcl_GetCommandName(menuPtr->interp, menuPtr->widgetCmd));
  2204.     }
  2205.     if (menuPtr->flags & REDRAW_PENDING) {
  2206.         Tcl_CancelIdleCall(DisplayMenu, (ClientData) menuPtr);
  2207.     }
  2208.     if (menuPtr->flags & RESIZE_PENDING) {
  2209.         Tcl_CancelIdleCall(ComputeMenuGeometry, (ClientData) menuPtr);
  2210.     }
  2211.     Tcl_EventuallyFree((ClientData) menuPtr, DestroyMenu);
  2212.     }
  2213. }
  2214.  
  2215. /*
  2216.  *----------------------------------------------------------------------
  2217.  *
  2218.  * MenuCmdDeletedProc --
  2219.  *
  2220.  *    This procedure is invoked when a widget command is deleted.  If
  2221.  *    the widget isn't already in the process of being destroyed,
  2222.  *    this command destroys it.
  2223.  *
  2224.  * Results:
  2225.  *    None.
  2226.  *
  2227.  * Side effects:
  2228.  *    The widget is destroyed.
  2229.  *
  2230.  *----------------------------------------------------------------------
  2231.  */
  2232.  
  2233. static void
  2234. MenuCmdDeletedProc(clientData)
  2235.     ClientData clientData;    /* Pointer to widget record for widget. */
  2236. {
  2237.     Menu *menuPtr = (Menu *) clientData;
  2238.     Tk_Window tkwin = menuPtr->tkwin;
  2239.  
  2240.     /*
  2241.      * This procedure could be invoked either because the window was
  2242.      * destroyed and the command was then deleted (in which case tkwin
  2243.      * is NULL) or because the command was deleted, and then this procedure
  2244.      * destroys the widget.
  2245.      */
  2246.  
  2247.     if (tkwin != NULL) {
  2248.     menuPtr->tkwin = NULL;
  2249.     Tk_DestroyWindow(tkwin);
  2250.     }
  2251. }
  2252.  
  2253. /*
  2254.  *----------------------------------------------------------------------
  2255.  *
  2256.  * MenuNewEntry --
  2257.  *
  2258.  *    This procedure allocates and initializes a new menu entry.
  2259.  *
  2260.  * Results:
  2261.  *    The return value is a pointer to a new menu entry structure,
  2262.  *    which has been malloc-ed, initialized, and entered into the
  2263.  *    entry array for the  menu.
  2264.  *
  2265.  * Side effects:
  2266.  *    Storage gets allocated.
  2267.  *
  2268.  *----------------------------------------------------------------------
  2269.  */
  2270.  
  2271. static MenuEntry *
  2272. MenuNewEntry(menuPtr, index, type)
  2273.     Menu *menuPtr;        /* Menu that will hold the new entry. */
  2274.     int index;            /* Where in the menu the new entry is to
  2275.                  * go. */
  2276.     int type;            /* The type of the new entry. */
  2277. {
  2278.     MenuEntry *mePtr;
  2279.     MenuEntry **newEntries;
  2280.     int i;
  2281.  
  2282.     /*
  2283.      * Create a new array of entries with an empty slot for the
  2284.      * new entry.
  2285.      */
  2286.  
  2287.     newEntries = (MenuEntry **) ckalloc((unsigned)
  2288.         ((menuPtr->numEntries+1)*sizeof(MenuEntry *)));
  2289.     for (i = 0; i < index; i++) {
  2290.     newEntries[i] = menuPtr->entries[i];
  2291.     }
  2292.     for (  ; i < menuPtr->numEntries; i++) {
  2293.     newEntries[i+1] = menuPtr->entries[i];
  2294.     }
  2295.     if (menuPtr->numEntries != 0) {
  2296.     ckfree((char *) menuPtr->entries);
  2297.     }
  2298.     menuPtr->entries = newEntries;
  2299.     menuPtr->numEntries++;
  2300.     menuPtr->entries[index] = mePtr = (MenuEntry *) ckalloc(sizeof(MenuEntry));
  2301.     mePtr->type = type;
  2302.     mePtr->menuPtr = menuPtr;
  2303.     mePtr->label = NULL;
  2304.     mePtr->labelLength = 0;
  2305.     mePtr->underline = -1;
  2306.     mePtr->bitmap = None;
  2307.     mePtr->imageString = NULL;
  2308.     mePtr->image = NULL;
  2309.     mePtr->selectImageString  = NULL;
  2310.     mePtr->selectImage = NULL;
  2311.     mePtr->accel = NULL;
  2312.     mePtr->accelLength = 0;
  2313.     mePtr->state = tkNormalUid;
  2314.     mePtr->height = 0;
  2315.     mePtr->y = 0;
  2316.     mePtr->indicatorDiameter = 0;
  2317.     mePtr->border = NULL;
  2318.     mePtr->fg = NULL;
  2319.     mePtr->activeBorder = NULL;
  2320.     mePtr->activeFg = NULL;
  2321.     mePtr->fontPtr = NULL;
  2322.     mePtr->textGC = None;
  2323.     mePtr->activeGC = None;
  2324.     mePtr->disabledGC = None;
  2325.     mePtr->indicatorOn = 1;
  2326.     mePtr->indicatorFg = NULL;
  2327.     mePtr->indicatorGC = None;
  2328.     mePtr->command = NULL;
  2329.     mePtr->name = NULL;
  2330.     mePtr->onValue = NULL;
  2331.     mePtr->offValue = NULL;
  2332.     mePtr->flags = 0;
  2333.     return mePtr;
  2334. }
  2335.  
  2336. /*
  2337.  *----------------------------------------------------------------------
  2338.  *
  2339.  * MenuAddOrInsert --
  2340.  *
  2341.  *    This procedure does all of the work of the "add" and "insert"
  2342.  *    widget commands, allowing the code for these to be shared.
  2343.  *
  2344.  * Results:
  2345.  *    A standard Tcl return value.
  2346.  *
  2347.  * Side effects:
  2348.  *    A new menu entry is created in menuPtr.
  2349.  *
  2350.  *----------------------------------------------------------------------
  2351.  */
  2352.  
  2353. static int
  2354. MenuAddOrInsert(interp, menuPtr, indexString, argc, argv)
  2355.     Tcl_Interp *interp;            /* Used for error reporting. */
  2356.     Menu *menuPtr;            /* Widget in which to create new
  2357.                      * entry. */
  2358.     char *indexString;            /* String describing index at which
  2359.                      * to insert.  NULL means insert at
  2360.                      * end. */
  2361.     int argc;                /* Number of elements in argv. */
  2362.     char **argv;            /* Arguments to command:  first arg
  2363.                      * is type of entry, others are
  2364.                      * config options. */
  2365. {
  2366.     int c, type, i, index;
  2367.     size_t length;
  2368.     MenuEntry *mePtr;
  2369.  
  2370.     if (indexString != NULL) {
  2371.     if (GetMenuIndex(interp, menuPtr, indexString, 1, &index) != TCL_OK) {
  2372.         return TCL_ERROR;
  2373.     }
  2374.     } else {
  2375.     index = menuPtr->numEntries;
  2376.     }
  2377.     if (index < 0) {
  2378.     Tcl_AppendResult(interp, "bad index \"", indexString, "\"",
  2379.          (char *) NULL);
  2380.     return TCL_ERROR;
  2381.     }
  2382.     if (menuPtr->tearOff && (index == 0)) {
  2383.     index = 1;
  2384.     }
  2385.  
  2386.     /*
  2387.      * Figure out the type of the new entry.
  2388.      */
  2389.  
  2390.     c = argv[0][0];
  2391.     length = strlen(argv[0]);
  2392.     if ((c == 'c') && (strncmp(argv[0], "cascade", length) == 0)
  2393.         && (length >= 2)) {
  2394.     type = CASCADE_ENTRY;
  2395.     } else if ((c == 'c') && (strncmp(argv[0], "checkbutton", length) == 0)
  2396.         && (length >= 2)) {
  2397.     type = CHECK_BUTTON_ENTRY;
  2398.     } else if ((c == 'c') && (strncmp(argv[0], "command", length) == 0)
  2399.         && (length >= 2)) {
  2400.     type = COMMAND_ENTRY;
  2401.     } else if ((c == 'r')
  2402.         && (strncmp(argv[0], "radiobutton", length) == 0)) {
  2403.     type = RADIO_BUTTON_ENTRY;
  2404.     } else if ((c == 's')
  2405.         && (strncmp(argv[0], "separator", length) == 0)) {
  2406.     type = SEPARATOR_ENTRY;
  2407.     } else {
  2408.     Tcl_AppendResult(interp, "bad menu entry type \"",
  2409.         argv[0], "\": must be cascade, checkbutton, ",
  2410.         "command, radiobutton, or separator", (char *) NULL);
  2411.     return TCL_ERROR;
  2412.     }
  2413.     mePtr = MenuNewEntry(menuPtr, index, type);
  2414.     if (ConfigureMenuEntry(interp, menuPtr, mePtr, index,
  2415.         argc-1, argv+1, 0) != TCL_OK) {
  2416.     DestroyMenuEntry((ClientData) mePtr);
  2417.     for (i = index+1; i < menuPtr->numEntries; i++) {
  2418.         menuPtr->entries[i-1] = menuPtr->entries[i];
  2419.     }
  2420.     menuPtr->numEntries--;
  2421.     return TCL_ERROR;
  2422.     }
  2423.     return TCL_OK;
  2424. }
  2425.  
  2426. /*
  2427.  *--------------------------------------------------------------
  2428.  *
  2429.  * MenuVarProc --
  2430.  *
  2431.  *    This procedure is invoked when someone changes the
  2432.  *    state variable associated with a radiobutton or checkbutton
  2433.  *    menu entry.  The entry's selected state is set to match
  2434.  *    the value of the variable.
  2435.  *
  2436.  * Results:
  2437.  *    NULL is always returned.
  2438.  *
  2439.  * Side effects:
  2440.  *    The menu entry may become selected or deselected.
  2441.  *
  2442.  *--------------------------------------------------------------
  2443.  */
  2444.  
  2445.     /* ARGSUSED */
  2446. static char *
  2447. MenuVarProc(clientData, interp, name1, name2, flags)
  2448.     ClientData clientData;    /* Information about menu entry. */
  2449.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  2450.     char *name1;        /* First part of variable's name. */
  2451.     char *name2;        /* Second part of variable's name. */
  2452.     int flags;            /* Describes what just happened. */
  2453. {
  2454.     MenuEntry *mePtr = (MenuEntry *) clientData;
  2455.     Menu *menuPtr;
  2456.     char *value;
  2457.  
  2458.     menuPtr = mePtr->menuPtr;
  2459.  
  2460.     /*
  2461.      * If the variable is being unset, then re-establish the
  2462.      * trace unless the whole interpreter is going away.
  2463.      */
  2464.  
  2465.     if (flags & TCL_TRACE_UNSETS) {
  2466.     mePtr->flags &= ~ENTRY_SELECTED;
  2467.     if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
  2468.         Tcl_TraceVar(interp, mePtr->name,
  2469.             TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  2470.             MenuVarProc, clientData);
  2471.     }
  2472.     EventuallyRedrawMenu(menuPtr, (MenuEntry *) NULL);
  2473.     return (char *) NULL;
  2474.     }
  2475.  
  2476.     /*
  2477.      * Use the value of the variable to update the selected status of
  2478.      * the menu entry.
  2479.      */
  2480.  
  2481.     value = Tcl_GetVar(interp, mePtr->name, TCL_GLOBAL_ONLY);
  2482.     if (value == NULL) {
  2483.     value = "";
  2484.     }
  2485.     if (strcmp(value, mePtr->onValue) == 0) {
  2486.     if (mePtr->flags & ENTRY_SELECTED) {
  2487.         return (char *) NULL;
  2488.     }
  2489.     mePtr->flags |= ENTRY_SELECTED;
  2490.     } else if (mePtr->flags & ENTRY_SELECTED) {
  2491.     mePtr->flags &= ~ENTRY_SELECTED;
  2492.     } else {
  2493.     return (char *) NULL;
  2494.     }
  2495.     EventuallyRedrawMenu(menuPtr, (MenuEntry *) NULL);
  2496.     return (char *) NULL;
  2497. }
  2498.  
  2499. /*
  2500.  *----------------------------------------------------------------------
  2501.  *
  2502.  * EventuallyRedrawMenu --
  2503.  *
  2504.  *    Arrange for an entry of a menu, or the whole menu, to be
  2505.  *    redisplayed at some point in the future.
  2506.  *
  2507.  * Results:
  2508.  *    None.
  2509.  *
  2510.  * Side effects:
  2511.  *    A when-idle hander is scheduled to do the redisplay, if there
  2512.  *    isn't one already scheduled.
  2513.  *
  2514.  *----------------------------------------------------------------------
  2515.  */
  2516.  
  2517. static void
  2518. EventuallyRedrawMenu(menuPtr, mePtr)
  2519.     register Menu *menuPtr;    /* Information about menu to redraw. */
  2520.     register MenuEntry *mePtr;    /* Entry to redraw.  NULL means redraw
  2521.                  * all the entries in the menu. */
  2522. {
  2523.     int i;
  2524.     if (menuPtr->tkwin == NULL) {
  2525.     return;
  2526.     }
  2527.     if (mePtr != NULL) {
  2528.     mePtr->flags |= ENTRY_NEEDS_REDISPLAY;
  2529.     } else {
  2530.     for (i = 0; i < menuPtr->numEntries; i++) {
  2531.         menuPtr->entries[i]->flags |= ENTRY_NEEDS_REDISPLAY;
  2532.     }
  2533.     }
  2534.     if ((menuPtr->tkwin == NULL) || !Tk_IsMapped(menuPtr->tkwin)
  2535.         || (menuPtr->flags & REDRAW_PENDING)) {
  2536.     return;
  2537.     }
  2538.     Tcl_DoWhenIdle(DisplayMenu, (ClientData) menuPtr);
  2539.     menuPtr->flags |= REDRAW_PENDING;
  2540. }
  2541.  
  2542. /*
  2543.  *--------------------------------------------------------------
  2544.  *
  2545.  * PostSubmenu --
  2546.  *
  2547.  *    This procedure arranges for a particular submenu (i.e. the
  2548.  *    menu corresponding to a given cascade entry) to be
  2549.  *    posted.
  2550.  *
  2551.  * Results:
  2552.  *    A standard Tcl return result.  Errors may occur in the
  2553.  *    Tcl commands generated to post and unpost submenus.
  2554.  *
  2555.  * Side effects:
  2556.  *    If there is already a submenu posted, it is unposted.
  2557.  *    The new submenu is then posted.
  2558.  *
  2559.  *--------------------------------------------------------------
  2560.  */
  2561.  
  2562. static int
  2563. PostSubmenu(interp, menuPtr, mePtr)
  2564.     Tcl_Interp *interp;        /* Used for invoking sub-commands and
  2565.                  * reporting errors. */
  2566.     register Menu *menuPtr;    /* Information about menu as a whole. */
  2567.     register MenuEntry *mePtr;    /* Info about submenu that is to be
  2568.                  * posted.  NULL means make sure that
  2569.                  * no submenu is posted. */
  2570. {
  2571.     char string[30];
  2572.     int result, x, y;
  2573.     Tk_Window tkwin;
  2574.  
  2575.     if (mePtr == menuPtr->postedCascade) {
  2576.     return TCL_OK;
  2577.     }
  2578.  
  2579.     if (menuPtr->postedCascade != NULL) {
  2580.     /*
  2581.      * Note: when unposting a submenu, we have to redraw the entire
  2582.      * parent menu.  This is because of a combination of the following
  2583.      * things:
  2584.      * (a) the submenu partially overlaps the parent.
  2585.      * (b) the submenu specifies "save under", which causes the X
  2586.      *     server to make a copy of the information under it when it
  2587.      *     is posted.  When the submenu is unposted, the X server
  2588.      *     copies this data back and doesn't generate any Expose
  2589.      *     events for the parent.
  2590.      * (c) the parent may have redisplayed itself after the submenu
  2591.      *     was posted, in which case the saved information is no
  2592.      *     longer correct.
  2593.      * The simplest solution is just force a complete redisplay of
  2594.      * the parent.
  2595.      */
  2596.  
  2597.     EventuallyRedrawMenu(menuPtr, (MenuEntry *) NULL);
  2598.     result = Tcl_VarEval(interp, menuPtr->postedCascade->name,
  2599. #ifdef STk_CODE
  2600.         " 'unpost", (char *) NULL);
  2601. #else
  2602.         " unpost", (char *) NULL);
  2603. #endif
  2604.     menuPtr->postedCascade = NULL;
  2605.     if (result != TCL_OK) {
  2606.         return result;
  2607.     }
  2608.     }
  2609.  
  2610.     if ((mePtr != NULL) && (mePtr->name != NULL)
  2611.         && Tk_IsMapped(menuPtr->tkwin)) {
  2612.     /*
  2613.      * Make sure that the cascaded submenu is a child of the
  2614.      * parent menu.
  2615.      */
  2616.  
  2617. #ifdef STk_CODE
  2618.       /* For STk, the submenu .a.b.c is stored as #..a.b.c
  2619.        * We have to skip the '#.'
  2620.        * This ugly and should be changed 
  2621.        */
  2622.         tkwin = Tk_NameToWindow(interp, mePtr->name+2, menuPtr->tkwin);
  2623. #else      
  2624.     tkwin = Tk_NameToWindow(interp, mePtr->name, menuPtr->tkwin);
  2625. #endif
  2626.     if (tkwin == NULL) {
  2627.         return TCL_ERROR;
  2628.     }
  2629.     if (Tk_Parent(tkwin) != menuPtr->tkwin) {
  2630.         Tcl_AppendResult(interp, "cascaded sub-menu ",
  2631.             Tk_PathName(tkwin), " must be a child of ",
  2632.             Tk_PathName(menuPtr->tkwin), (char *) NULL);
  2633.         return TCL_ERROR;
  2634.     }
  2635.  
  2636.     /*
  2637.      * Position the cascade with its upper left corner slightly
  2638.      * below and to the left of the upper right corner of the
  2639.      * menu entry (this is an attempt to match Motif behavior).
  2640.      */
  2641.     Tk_GetRootCoords(menuPtr->tkwin, &x, &y);
  2642.     x += Tk_Width(menuPtr->tkwin) - menuPtr->borderWidth
  2643.         - menuPtr->activeBorderWidth - 2;
  2644.     y += mePtr->y + menuPtr->activeBorderWidth + 2;
  2645.     sprintf(string, "%d %d", x, y);
  2646. #ifdef STk_CODE
  2647.     result = Tcl_VarEval(interp, mePtr->name, " 'post ", string,
  2648. #else
  2649.     result = Tcl_VarEval(interp, mePtr->name, " post ", string,
  2650. #endif
  2651.         (char *) NULL);
  2652.     if (result != TCL_OK) {
  2653.         return result;
  2654.     }
  2655.     menuPtr->postedCascade = mePtr;
  2656.     }
  2657.     return TCL_OK;
  2658. }
  2659.  
  2660. /*
  2661.  *----------------------------------------------------------------------
  2662.  *
  2663.  * ActivateMenuEntry --
  2664.  *
  2665.  *    This procedure is invoked to make a particular menu entry
  2666.  *    the active one, deactivating any other entry that might
  2667.  *    currently be active.
  2668.  *
  2669.  * Results:
  2670.  *    The return value is a standard Tcl result (errors can occur
  2671.  *    while posting and unposting submenus).
  2672.  *
  2673.  * Side effects:
  2674.  *    Menu entries get redisplayed, and the active entry changes.
  2675.  *    Submenus may get posted and unposted.
  2676.  *
  2677.  *----------------------------------------------------------------------
  2678.  */
  2679.  
  2680. static int
  2681. ActivateMenuEntry(menuPtr, index)
  2682.     register Menu *menuPtr;        /* Menu in which to activate. */
  2683.     int index;                /* Index of entry to activate, or
  2684.                      * -1 to deactivate all entries. */
  2685. {
  2686.     register MenuEntry *mePtr;
  2687.     int result = TCL_OK;
  2688.  
  2689.     if (menuPtr->active >= 0) {
  2690.     mePtr = menuPtr->entries[menuPtr->active];
  2691.  
  2692.     /*
  2693.      * Don't change the state unless it's currently active (state
  2694.      * might already have been changed to disabled).
  2695.      */
  2696.  
  2697.     if (mePtr->state == tkActiveUid) {
  2698.         mePtr->state = tkNormalUid;
  2699.     }
  2700.     EventuallyRedrawMenu(menuPtr, menuPtr->entries[menuPtr->active]);
  2701.     }
  2702.     menuPtr->active = index;
  2703.     if (index >= 0) {
  2704.     mePtr = menuPtr->entries[index];
  2705.     mePtr->state = tkActiveUid;
  2706.     EventuallyRedrawMenu(menuPtr, mePtr);
  2707.     }
  2708.     return result;
  2709. }
  2710.  
  2711. /*
  2712.  *----------------------------------------------------------------------
  2713.  *
  2714.  * MenuImageProc --
  2715.  *
  2716.  *    This procedure is invoked by the image code whenever the manager
  2717.  *    for an image does something that affects the size of contents
  2718.  *    of an image displayed in a menu entry.
  2719.  *
  2720.  * Results:
  2721.  *    None.
  2722.  *
  2723.  * Side effects:
  2724.  *    Arranges for the menu to get redisplayed.
  2725.  *
  2726.  *----------------------------------------------------------------------
  2727.  */
  2728.  
  2729. static void
  2730. MenuImageProc(clientData, x, y, width, height, imgWidth, imgHeight)
  2731.     ClientData clientData;        /* Pointer to widget record. */
  2732.     int x, y;                /* Upper left pixel (within image)
  2733.                          * that must be redisplayed. */
  2734.     int width, height;        /* Dimensions of area to redisplay
  2735.                      * (may be <= 0). */
  2736.     int imgWidth, imgHeight;        /* New dimensions of image. */
  2737. {
  2738.     register Menu *menuPtr = ((MenuEntry *) clientData)->menuPtr;
  2739.  
  2740.     if ((menuPtr->tkwin != NULL) && !(menuPtr->flags & RESIZE_PENDING)) {
  2741.     menuPtr->flags |= RESIZE_PENDING;
  2742.     Tcl_DoWhenIdle(ComputeMenuGeometry, (ClientData) menuPtr);
  2743.     }
  2744. }
  2745.  
  2746. /*
  2747.  *----------------------------------------------------------------------
  2748.  *
  2749.  * MenuSelectImageProc --
  2750.  *
  2751.  *    This procedure is invoked by the image code whenever the manager
  2752.  *    for an image does something that affects the size of contents
  2753.  *    of an image displayed in a menu entry when it is selected.
  2754.  *
  2755.  * Results:
  2756.  *    None.
  2757.  *
  2758.  * Side effects:
  2759.  *    Arranges for the menu to get redisplayed.
  2760.  *
  2761.  *----------------------------------------------------------------------
  2762.  */
  2763.  
  2764. static void
  2765. MenuSelectImageProc(clientData, x, y, width, height, imgWidth, imgHeight)
  2766.     ClientData clientData;        /* Pointer to widget record. */
  2767.     int x, y;                /* Upper left pixel (within image)
  2768.                      * that must be redisplayed. */
  2769.     int width, height;            /* Dimensions of area to redisplay
  2770.                      * (may be <= 0). */
  2771.     int imgWidth, imgHeight;        /* New dimensions of image. */
  2772. {
  2773.     register MenuEntry *mePtr = (MenuEntry *) clientData;
  2774.  
  2775.     if ((mePtr->flags & ENTRY_SELECTED)
  2776.         && !(mePtr->menuPtr->flags & REDRAW_PENDING)) {
  2777.     mePtr->menuPtr->flags |= REDRAW_PENDING;
  2778.     Tcl_DoWhenIdle(DisplayMenu, (ClientData) mePtr->menuPtr);
  2779.     }
  2780. }
  2781.  
  2782.